0

I want to enable making several concurrent requests to server from a single client. How can this be done with .Net WebApi? In ASP.Net MVC 3 it could be enabled with adding the follow attribute to the controller:

[SessionState(SessionStateBehavior.ReadOnly)]
public class SomeController : AsyncController
{
  //.....
}

Information about MVC Session was found here:

The Downsides of ASP.NET Session State

But in Web Api application it doesn't work. I've also accessing session data like in this question:

Accessing Session Using ASP.NET Web API

So, I think thats why I can't make multiple requests at once from client. What should I do enable that feature?

John Mitchell
  • 9,653
  • 9
  • 57
  • 91
Igor Konopko
  • 764
  • 2
  • 13
  • 26

2 Answers2

2

Resolved by adding:

void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
        {
            HttpContext.Current.SetSessionStateBehavior(
                SessionStateBehavior.ReadOnly);
        }

and

public override void Init()
        {
            PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
            base.Init();
        }

to Global.asax

Igor Konopko
  • 764
  • 2
  • 13
  • 26
  • +1 The question was difficult to parse, but the answer was just what I was looking for. – mrK Sep 01 '14 at 01:21
1

Yes, there are a lot of problems with using session in your ASP.NET applications. Moreover, by default, HTTP (and by extension, REST- like/style application) is stateless – and as a result each HTTP request should carry enough information by itself for its recipient to process it to be in complete harmony with the stateless nature of HTTP.

Web Api was not design to support Asp.net Session ; you have nothing else to do. So, If you need some information in a Web Api, do not rely on Seesion State but add them into your request

What is very funny, is that some people want to support Session in Web Api ...

Cybermaxs
  • 24,378
  • 8
  • 83
  • 112
  • I've add session access like in this question http://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api But if there is no session at web.api, why I can't make several requests at once? – Igor Konopko May 17 '13 at 13:31
  • So... if there is no session like in mvc controllers, how to enable several requests at once? – Igor Konopko May 17 '13 at 13:35
  • not sure to clearly understand your question : do you want to use session or not. You can do multiple request at the same time but you can't use Session variables, because the session will always be null – Cybermaxs May 17 '13 at 13:37
  • thanks, it's not clearer. Does your Web Api relies on Session variables ? – Cybermaxs May 17 '13 at 13:50
  • Yes, In my application is necessary to use clients SessionId and user.Identity – Igor Konopko May 17 '13 at 13:52