How to execute async ‘fire and forget’ operation in ASP.NET Web API

So, I have this Web API action, that executes an asynchronous function. public void Post([FromBody]InsertRequest request) { InsertPostCommand.Execute(request); DoAsync(); } private async void DoAsync() { await Task.Run(() => { //do async stuff here }); } I actually want the controller action to return to the client whilst the async operation executes. I did this and … Read more

Preserve HttpContext when going async with WebAPI (Medium Trust)

I am building a set of ASP.Net hosted WebAPI services that must use an old library which depends heavily on HttpContext.Current. I am having trouble ensuring that context is preserved in all the methods that participate in an async call. I have tried several variations with await/Task.Wait and TaskScheduler.FromCurrentSynchronizationContext() on the below code.

Wrapping synchronous code into asynchronous call

I have a method in ASP.NET application, that consumes quite a lot of time to complete. A call to this method might occur up to 3 times during one user request, depending on the cache state and parameters that user provides. Each call takes about 1-2 seconds to complete. The method itself is synchronous call to the service and there is no possibility to override the implementation.
So the synchronous call to the service looks something like the following: