How to answer a request but continue processing code in WebApi How to answer a request but continue processing code in WebApi multithreading multithreading

How to answer a request but continue processing code in WebApi


I would like to answer a request, but continue processing code.

Are you sure you want to do this in ASP.NET? This is not a situation that ASP.NET (or any web server) was designed to handle.

The classic (correct) way to do this is to queue the work to a persistent queue and have a separate backend process do the actual processing of that work.

There are all kinds of dangers with doing processing inside of ASP.NET outside of a request context. In general, you can't assume that the work will ever actually be done. If you're OK with that (or just like to live dangerously), then you can use HostingEnvironment.QueueBackgroundWorkItem.

I have a blog post that goes into more detail.