Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
295 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...