简体   繁体   中英

Alternative Session/static vars ASP.NET/C#

I'd like to know if there is a valid Session or static in .NET environment. What I need is a variable to store some data that maintans its value, accessible through all ASP.NET pages / C# forms but not unique for all users that use the application .

Then your answer would likely be Application Cache. Look at System.Web.Caching

Since there is some discussion about lifetime of the cache object, you can set it. The user was not specific, however in how long this has to live. Between application recycles? during the lifetime of a single instance, does it need to be between instances. Cache will work for the most simple requirement.

I would think you are looking for Application State rather than Application Cache. Application State would work the same way like session state but would be shared between all sessions.

Sample usage:

 Application["Message"] = "Welcome to my Website"; 

More information: http://www.codeproject.com/Articles/87316/A-walkthrough-to-Application-State

Can you use the application cache? it is shared for all users throughout your application, accessible from all pages, and not unique per user.

please see this and this for more details on how to use the caching API

You could try handling this with an Application-scoped variable.

First in your Global.asax file, add the following:

void Application_Start(object sender, EventArgs e)
{
    [Other Stuff Already Here Omitted]
    Application["MyVariable"] = "My Default value";
}

Then on any page, you can access this:

var myVar = Application["MyVariable"];

You may expose a class as Static. That'll make it available across all Sessions, but restricted to that Application instance.

The answer is no. The exists no ".Net environment" on that level. Your .Net code gets runtime restrictions from the host also. Lets define two hosts (there are more): IIS and "simply main exe".

For IIS hosted code, you are operating in an environment where the host is creating a thread (or rather allocating one) and executing your code on this thread. The host itself defines when and if to reset the process. You cannot, or rather should not, assume anything about which process or thread is executing your code. This gets enhanced when you talk about iis in a farm.

For simple main code in an exe, the operating system creates the process and initial first thread and the sets the execution pointer to the first statement in the main function.

"static" maintains state throughout the lifetime of the owning process, that is a guarantee given to you by .Net.

"Session" is specific implementation to handle much more than static properties.

I would recommend a refinement of the question where you also describe your host. For iis, the answer could be AppFabric cache.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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