那么asp.net web应用程序中的静态类以及它如何作为会话或应用程序变量工作?

时间:2021-12-17 03:35:40

If i stored the user information in static class , it will be overwrite by another user once they logged in . how static class act in web application in asp.Net ?

如果我将用户信息存储在静态类中,则一旦用户登录,它将被另一个用户覆盖。静态类如何在asp.Net中的web应用程序中运行?

2 个解决方案

#1


2  

A static class behaves in ASP.NET in the same way as in any other environment: .NET applications run inside a sandbox called AppDomainand static values are available withint the AppDomain.

静态类在ASP.NET中的行为方式与在任何其他环境中的行为相同:.NET应用程序在名为AppDomain的沙箱中运行,而静态值在AppDomain上可用。

In other words: a static value isn't thread-scoped, and this means that it's an application domain-wide concern. That is, any user from any thread can modify a static value.

换句话说:静态值不是线程范围的,这意味着它是应用程序域范围内的问题。也就是说,来自任何线程的任何用户都可以修改静态值。

There's an exception to the rule: the [ThreadStatic] attribute, which can be applied to static class fields and its values will be only available in the thread that they were created, and if the thread ends, the so-called values are lost. Anyway, don't use [ThreadStatic] in ASP.NET, because the Web server (like IIS/OWIN) may process a requests using more than a thread.

该规则有一个例外:[ThreadStatic]属性,可以应用于静态类字段,其值只能在创建它们的线程中使用,如果线程结束,则所谓的值将丢失。无论如何,不​​要在ASP.NET中使用[ThreadStatic],因为Web服务器(如IIS / OWIN)可能使用多个线程处理请求。

#2


0  

The values you add to a static field in a class are defined for all instances of that class.

您为类中的静态字段添加的值是为该类的所有实例定义的。

If you store user information in a static field, it will be shared by all the instances of your class in the running web process. Every requests will read/write the value of this static field. You should not do that because it will lead to incorrect data.

如果将用户信息存储在静态字段中,则将在运行的Web进程中由类的所有实例共享该信息。每个请求都将读取/写入此静态字段的值。你不应该这样做,因为它会导致不正确的数据。

In a web application, you will have a lot of multiple requests that happens at the same time so it is for this reason you need to use the asp .net session object.

在Web应用程序中,您将同时发生许多多个请求,因此您需要使用asp .net会话对象。

The information you store in the session object is guaranted to be unique for each user sessions by the runtime.

存储在会话对象中的信息将保证为运行时的每个用户会话都是唯一的。

#1


2  

A static class behaves in ASP.NET in the same way as in any other environment: .NET applications run inside a sandbox called AppDomainand static values are available withint the AppDomain.

静态类在ASP.NET中的行为方式与在任何其他环境中的行为相同:.NET应用程序在名为AppDomain的沙箱中运行,而静态值在AppDomain上可用。

In other words: a static value isn't thread-scoped, and this means that it's an application domain-wide concern. That is, any user from any thread can modify a static value.

换句话说:静态值不是线程范围的,这意味着它是应用程序域范围内的问题。也就是说,来自任何线程的任何用户都可以修改静态值。

There's an exception to the rule: the [ThreadStatic] attribute, which can be applied to static class fields and its values will be only available in the thread that they were created, and if the thread ends, the so-called values are lost. Anyway, don't use [ThreadStatic] in ASP.NET, because the Web server (like IIS/OWIN) may process a requests using more than a thread.

该规则有一个例外:[ThreadStatic]属性,可以应用于静态类字段,其值只能在创建它们的线程中使用,如果线程结束,则所谓的值将丢失。无论如何,不​​要在ASP.NET中使用[ThreadStatic],因为Web服务器(如IIS / OWIN)可能使用多个线程处理请求。

#2


0  

The values you add to a static field in a class are defined for all instances of that class.

您为类中的静态字段添加的值是为该类的所有实例定义的。

If you store user information in a static field, it will be shared by all the instances of your class in the running web process. Every requests will read/write the value of this static field. You should not do that because it will lead to incorrect data.

如果将用户信息存储在静态字段中,则将在运行的Web进程中由类的所有实例共享该信息。每个请求都将读取/写入此静态字段的值。你不应该这样做,因为它会导致不正确的数据。

In a web application, you will have a lot of multiple requests that happens at the same time so it is for this reason you need to use the asp .net session object.

在Web应用程序中,您将同时发生许多多个请求,因此您需要使用asp .net会话对象。

The information you store in the session object is guaranted to be unique for each user sessions by the runtime.

存储在会话对象中的信息将保证为运行时的每个用户会话都是唯一的。