0

I am using ASP.NET Web Api with basic authentication.

My problem is that multiple API calls are not handled concurrently.

Let's say we call this simple method multiple times from a single machine with the same user credentials. If you run it 5 times in one second, the overall processing time will be 25 seconds instead of 5.

    [System.Web.Http.HttpGet]
    [System.Web.Http.Authorize(Roles = @"domain\group")]
    public string Test()
    {
        var startTime = DateTime.Now;
        System.Threading.Thread.Sleep(5000);
        var endTime = DateTime.Now;
        return $"{startTime} {endTime}";
    }

The answers to this question didn't help me:

I think it is because of the basic authentication.

I also tried changing the test method to async like this:

    [System.Web.Http.HttpGet]
    [System.Web.Http.Authorize(Roles = @"domain\group")]
    public async Task<string> Test()
    {
        var startTime = DateTime.Now;
        await Task.Run(() => {
            System.Threading.Thread.Sleep(5000);
        });
        var endTime = DateTime.Now;
        return $"{startTime} {endTime}";
    }

I also tried coloring the ApiController with [SessionState(SessionStateBehavior.ReadOnly)].

How can I achieve concurrent calls? Or should I change authentication to something else (must be connected to AD groups!)?

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
Miroslav Adamec
  • 1,060
  • 1
  • 15
  • 23
  • _"Answers to Why is this web api controller not concurrent? doesn't seem to help me"_ - why not? Did you disable sessions? – CodeCaster May 03 '18 at 06:54
  • `My problem is that API calls are not concurrent.` How did you come to that conclusion? Where is the API being called from? – mjwills May 03 '18 at 06:56
  • @CodeCaster - I added to my web.config without success. – Miroslav Adamec May 03 '18 at 07:14
  • @mjwills API is called from local browser. If I call 2 requests at once I see, that startTime of 2nd request = endTime of 1st. On the other hand if I call one reqest from mozilla and 1 from chrome at once, they are processed concurrently. – Miroslav Adamec May 03 '18 at 07:16
  • 1
    Could you perhaps be running into https://stackoverflow.com/questions/985431/max-parallel-http-connections-in-a-browser ? How are you doing the local requests? Through AJAX? Something else? – mjwills May 03 '18 at 08:21

1 Answers1

0

@mjwills pointed me to the right direction. Code is perfectly OK. Problem was running concurrent requests from Chrome. When I wrote simple C# test application everything worked as expected.

mjwills
  • 23,389
  • 6
  • 40
  • 63
Miroslav Adamec
  • 1,060
  • 1
  • 15
  • 23