Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to answer a request but continue processing code in WebApi

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

I tried something like:

[HttpPost]
public async Task<HttpResponseMessage> SendAsync(MyRequest sms)
{
    await Task.Run(() => Process(sms)); //need to run in a separate thread
    var response = new MyRequest(sms) { Ack = true };
    return Request.CreateResponse(HttpStatusCode.Created, response.ToString());
}

private async void Process(MyRequest sms)
{
    var validationResult = new MyRequestValidation(_p2pContext, _carrierService).Validate(sms);
    if (string.IsNullOrWhiteSpace(validationResult.Errors[0].PropertyName)) // Request not valid
        return;

    Message msg;

    if (validationResult.IsValid)
    {
        msg = await _messageService.ProcessAsync(sms);
    }
    else // Create message as finished
    {
        msg = _messageService.MessageFromMyRequest(sms,
                finished: true,
                withEventSource: validationResult.Errors[0].CustomState.ToString()
              );
    }

    // Salve in db
    _p2pContext.MessageRepository.Create(msg);
    _p2pContext.Save();
}
like image 521
ridermansb Avatar asked Aug 12 '14 21:08

ridermansb


People also ask

How do I return a response in Web API?

Depending on which of these is returned, Web API uses a different mechanism to create the HTTP response. Convert directly to an HTTP response message. Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message. Write the serialized return value into the response body; return 200 (OK).

Can we have multiple get methods in Web API?

As mentioned, Web API controller can include multiple Get methods with different parameters and types. Let's add following action methods in StudentController to demonstrate how Web API handles multiple HTTP GET requests.


1 Answers

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.

like image 192
Stephen Cleary Avatar answered Nov 15 '22 13:11

Stephen Cleary