Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...