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

后端 未结 2 1549
礼貌的吻别
礼貌的吻别 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条回答
  •  旧时难觅i
    2020-12-09 13:13

    Do you try async Task ? Here is sample Controller:

    public class SendJobController : ApiController
        {
            public async Task> Post([FromBody] SendJobRequest request)
            {
                return await PostAsync(request);
            }
    
            private async Task> PostAsync(SendJobRequest request)
            {
                Task> t = new Task>(() =>
                {
                    ResponseEntity _response = new ResponseEntity();
    
                    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;
            }
        }
    

提交回复
热议问题