I just discovered why all ASP.Net websites are slow, and I am trying to work out what to do about it

前端 未结 10 661
情话喂你
情话喂你 2020-11-22 07:53

I just discovered that every request in an ASP.Net web application gets a Session lock at the beginning of a request, and then releases it at the end of the request!

相关标签:
10条回答
  • 2020-11-22 08:29

    Just to help anyone with this problem (locking requests when executing another one from the same session)...

    Today I started to solve this issue and, after some hours of research, I solved it by removing the Session_Start method (even if empty) from the Global.asax file.

    This works in all projects I've tested.

    0 讨论(0)
  • 2020-11-22 08:32

    Unless your application has specially needs, I think you have 2 approaches:

    1. Do not use session at all
    2. Use session as is and perform fine tuning as joel mentioned.

    Session is not only thread-safe but also state-safe, in a way that you know that until the current request is completed, every session variable wont change from another active request. In order for this to happen you must ensure that session WILL BE LOCKED until the current request have completed.

    You can create a session like behavior by many ways, but if it does not lock the current session, it wont be 'session'.

    For the specific problems you mentioned I think you should check HttpContext.Current.Response.IsClientConnected. This can be useful to to prevent unnecessary executions and waits on the client, although it cannot solve this problem entirely, as this can be used only by a pooling way and not async.

    0 讨论(0)
  • 2020-11-22 08:37

    I started using the AngiesList.Redis.RedisSessionStateModule, which aside from using the (very fast) Redis server for storage (I'm using the windows port -- though there is also an MSOpenTech port), it does absolutely no locking on the session.

    In my opinion, if your application is structured in a reasonable way, this is not a problem. If you actually need locked, consistent data as part of the session, you should specifically implement a lock/concurrency check on your own.

    MS deciding that every ASP.NET session should be locked by default just to handle poor application design is a bad decision, in my opinion. Especially because it seems like most developers didn't/don't even realize sessions were locked, let alone that apps apparently need to be structured so you can do read-only session state as much as possible (opt-out, where possible).

    0 讨论(0)
  • 2020-11-22 08:37

    Marking a controller's session state as readonly or disabled will solve the problem.

    You can decorate a controller with the following attribute to mark it read-only:

    [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
    

    the System.Web.SessionState.SessionStateBehavior enum has the following values:

    • Default
    • Disabled
    • ReadOnly
    • Required
    0 讨论(0)
提交回复
热议问题