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

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


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.


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;        }    }