简体   繁体   English

销毁C#/ ASP.NET中的静态变量

[英]Destroy Static Variables in C#/ASP.NET

I am forced to use static variables because of the asp.net execution path. 由于asp.net执行路径,我被迫使用静态变量。 I have to have the same variables on the master page as well as the page being called. 我必须在母版页以及被调用的页上具有相同的变量。 Static variables make it very easy to carry over variables because they remain static. 静态变量非常容易携带变量,因为它们保持静态。 Unfortunately these variables hang in the ether of memory because the app hasn't actually "exited". 不幸的是,这些变量挂在内存中,因为该应用程序实际上并未“退出”。 Is there a way to destroy these variables when I finished used them? 使用完这些变量后,有没有办法销毁这些变量? I looked into 我看着

IDisposable

but its implementation is not... clean. 但是它的实现不是很干净。 Is there a way to destroy variables when the page finishes rendering? 页面渲染完成后,有没有办法销毁变量?

I am forced to use static variables because of the asp.net execution path. 由于asp.net执行路径,我被迫使用静态变量。 I have to have the same variables on the master page as well as the page being called. 我必须在母版页以及被调用的页上具有相同的变量。 Static variables make it very easy to carry over variables because they remain static. 静态变量非常容易携带变量,因为它们保持静态。

You aren't "forced" to use static fields just to share data between a master page and its content page. 您不会“被迫”使用静态字段,而只是在母版页及其内容页之间共享数据。 You probably don't want to either: static fields will be shared between all requests from all users of your application, which means they need to be thread-safe and limited to data that is shared between all users. 您可能不想这样做:静态字段将在应用程序所有用户的所有请求之间共享,这意味着它们必须是线程安全的,并且仅限于所有用户之间共享的数据。

There are many techniques to share data between master and content page. 有许多技术可以在母版页和内容页之间共享数据。 For example, you could use the HttpContext.Items dictionary, which exists only for the duration of a single request. 例如,您可以使用HttpContext.Items字典,该字典仅在单个请求期间存在。

For anyone looking for a way to "pretend" static variables, here is how you can go about it: 对于任何寻求“假装”静态变量的方法的人,可以通过以下方法进行操作:

public static Datatype data
{
    get
    {
        return (Datatype)HttpContext.Current.Items["DATA"];
    }
    set
    {
        HttpContext.Current.Items["DATA"] = value;
    }
}

As far as I understand this doesn't necessary solve the "kill static" values, but it should avoid any data collisions caused by static variables. 据我了解,这不一定解决“杀死静态”值,但应避免由静态变量引起的任何数据冲突。 I had my entire project referencing the static variable and changing it would of created more messy code than should be seen. 我的整个项目都引用了静态变量,并对其进行了更改,从而创建了比应该看到的还要混乱的代码。 This way, when the call goes out to get "DATA" from your static object, it doesn't grab whatever pointed at but rather uses the context list which gets killed after and is unique to your session. 这样,当调用从静态对象中获取“ DATA”时,它不会捕获所指向的内容,而是使用上下文列表,该列表在会话之后被杀死,并且是会话唯一的。

You can set them to null . 您可以将它们设置为null If you're using ASP.Net WebForms you can do this inside the Page.Unload event handler. 如果使用的是ASP.Net WebForms,则可以在Page.Unload事件处理程序中执行此操作。

您应该避免在母版页中使用静态属性,而应使用Session [“”]变量,因为这些变量将在用户结束会话时被销毁。

Without knowing your setup, and thinking in a future of cloud environment as well, where you can have more than one server answering user requests and, you have no idea if the same server will answer all the requests of that user or the load balancer ask the other servers to continue, I would, as I currently do, give you 2 options 不知道您的设置,也不考虑未来的云环境,在那里您可以有多个服务器来回答用户请求,并且您不知道同一台服务器是否可以回答该用户的所有请求或负载均衡器询问其他服务器继续运行,我会像现在一样为您提供2种选择

Before the options, in a Web Environment, it is good pratice to stop using Sessions or Static variables (for the reason above) 在使用这些选项之前,在Web环境中,最好停止使用Sessions或Static变量(出于上述原因)

Use cookies 使用Cookie

Why not use a cookie to pass value from one place to the other and when consumed, simply write the expiration data back one year and the cookie is gone! 为什么不使用Cookie将值从一个地方传递到另一个地方,而在消费后,只需将过期数据写回一年,cookie就消失了!

Use MemCached 使用MemCached

I always have a Cache layer between my application and the database, and it's the best way to keep values, not only from the database to provide to all users, but a good way to keep variables when we need cross application. 我的应用程序和数据库之间总是有一个Cache层,这是保留值的最佳方法,不仅是从数据库提供给所有用户的值,而且是在需要跨应用程序时保留变量的好方法。

MemCached is free and runs fine on windows, plus there are several Cloud Services that offer this in their servers MemCached免费的,并且可以在Windows上正常运行,此外,在其服务器中也有一些Cloud Services提供此功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM