Your problem is that StartNew
doesn't work like Task.Run
with async
delegates. The return type of StartNew
is Task<Task>
(which is convertible to Task
). The "outer" Task
represents the beginning of the method, and the "inner" Task
represents the completion of the method (including any exceptions).
To get to the inner Task
, you can use Unwrap
. Or you can just use Task.Run
instead of StartNew
for async
code. LongRunning
is just an optimization hint and is really optional. Stephen Toub has a good blog post on the difference between StartNew
and Run
and why Run
is (usually) better for async
code.
Update from @usr comment below: LongRunning
only applies to the beginning of the async
method (up until the first incomplete operation is await
ed). So it's almost certainly better all around to use Task.Run
in this case.