Is working with the Session thread-safe?

后端 未结 4 2132
说谎
说谎 2021-02-02 10:57

Consider a user making multiple requests at the same time, do I have to lock all code that works with the Session?

If for example I have the following scenario, where in

4条回答
  •  眼角桃花
    2021-02-02 11:24

    As always, the answer depends on what you mean by "safety." In ASP .NET, each request gets exclusive access to its session state. This means that you don't have to worry about synchronizing access within the scope of a single request. If Session["user"] is non-null, then it will be non-null for the entire duration of the current request. In your example, request 1 will never throw a null reference exception.

提交回复
热议问题