Web API Service - How to make the requests at the server to be executed concurrently

后端 未结 2 1544
礼貌的吻别
礼貌的吻别 2020-12-09 12:54

I am using a WebApi rest service controller, hosted by IIS 7.5, as i understood from this post:

Are all the web requests executed in parallel and handled asynchrono

相关标签:
2条回答
  • 2020-12-09 13:13

    Do you try async Task ? Here is sample Controller:

    public class SendJobController : ApiController
        {
            public async Task<ResponseEntity<SendJobResponse>> Post([FromBody] SendJobRequest request)
            {
                return await PostAsync(request);
            }
    
            private async Task<ResponseEntity<SendJobResponse>> PostAsync(SendJobRequest request)
            {
                Task<ResponseEntity<SendJobResponse>> t = new Task<ResponseEntity<SendJobResponse>>(() =>
                {
                    ResponseEntity<SendJobResponse> _response = new ResponseEntity<SendJobResponse>();
    
                    try
                    {
                        //  
                        // some long process
                        // 
                        _response.responseStatus = "OK"; 
                        _response.responseMessage = "Success";
                        _response.responseObject = new SendJobResponse() { JobId = 1 };
    
                    }
                    catch (Exception ex)
                    {
                        _response.responseStatus = "ERROR"; 
                        _response.responseMessage = ex.Message;
                    }
    
                    return _response;
                });
    
                t.Start();
                return await t;
            }
        }
    
    0 讨论(0)
  • 2020-12-09 13:33

    Actually, disabling session state is the normal solution for web APIs. If you need it for some/all of your calls, you can call HttpContext.SetSessionStateBehavior (e.g., from Application_BeginRequest). Multiple read-only session state requests can run concurrently.

    0 讨论(0)
提交回复
热议问题