9

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 asynchronously?

A webApi service, by default, executes all its incoming requests in parallel, but only if the current multiple requests (at a certain time) came from different sessions. That is to say, if single client will send some simultaneously requests to server, all of them will be executed sequentially and won't be executed concurrently.

This behavior is a real problem for us, because in some cases, our client sends bunch of requests from different client's listeners, asynchronously (by browser), and all of them will actually be queued instead of being executed concurrently at the server. Therefore, in some cases, we experiencing a serious performance issues which are really noticeable at the client's web page.

How can we solve this problem? I understand we can maybe disable session state but that isn't a normal thing to do.

Community
  • 1
  • 1
AmirTNinja
  • 357
  • 3
  • 5
  • 15
  • 2
    Have you seen an `ASP.NET_SessionId` cookie being created? Is this a pure Web API project or are you using an `ApiController` in an MVC application? – user247702 May 26 '14 at 07:04
  • I'm using ApiController, which implements 'Post' method, and that's the method i'm accessing from my client browser by $.ajax command. Where can i see if 'ASP.NET_SessionId' cookie had been created for sure? at our chrome browser, in "Resources" at the developer tools panel, I've noticed we have a record of this cookie. – AmirTNinja May 26 '14 at 07:36
  • It's totally reasonable to disable session state if you don't use it. In fact, I disable it for several of my load balanced applications and use cookies to store preferences and other data which isn't secure in nature. Everything else is loaded when required. – a-h Aug 29 '14 at 13:20
  • The Web Browser limits the number of concurrent requests to a single domain too, so you might be seeing that: http://stackoverflow.com/questions/985431/max-parallel-http-connections-in-a-browser – a-h Aug 29 '14 at 13:22
  • It is not entirely accurate what you say about the default behavior of a WebApi service with regard to the ASP.NET Session State. Like @Stijn suggests, it's important to make a difference (to avoid confusion) between using and ApiController in an MVC.NET application on one hand, and a "pure" WebApi application on the other. In the latter, by default there is no session state, hence all requests execute in parallel (with other limitations applied). A read/write session state will be "blocking" as would any other "locking" resource. – Oskar Lindberg Nov 07 '14 at 08:20

2 Answers2

5

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.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
1

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;
        }
    }
Melih Mucuk
  • 6,988
  • 6
  • 37
  • 56