Web API allow only single async task

asp.net, asp.net-web-api, async-await, asynchronous, c#

Solution

`async` does not change the HTTP protocol (as I explain on my blog). So you still just get one response per request.

The proper solution is to save a "token" (and import data) for the work in some reliable storage (e.g., Azure table/queue), and have a separate processing backend that does the actual import.

The `ImportConfigurationData` action would then check whether a token already exists for that data, and fault the request if found.

Problem

What is a proper scenario for handling only single async action? For example I need to import large file and while it being imported I need to disable that option to ensure that second import not triggered. What comes in mind that: ``` [HttpPost] public async Task<HttpResponseMessage> ImportConfigurationData() { if (HttpContext.Current.Application["ImportConfigurationDataInProcess"] as bool? ?? false) return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Task still running"); HttpContext.Current.Application["ImportConfigurationDataInProcess"] = true; string root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); await Request.Content.ReadAsMultipartAsync(provider); //actual import HttpContext.Current.Application["ImportConfigurationDataInProcess"] = false; Request.CreateResponse(HttpStatusCode.OK, true) } ``` But it seems like very hard-coded solution. What is a proper way of handling that? Another thing it is not properly works on client side at it still waits for a response. So is it possible for user just to send that file to server and not wait unlit it will finishes but reload page after file sent to server without waiting while `await` stuff will finish.

Original source