ASP.NET HttpApplication local storage
I need some HttpApplication local storage. I thought the ApplicationState was the place for this, but apparently this may be shared across HttpApplication instances in an Appdomain.
public class MyHttpModule : IHttpModule
{
private object initializingLock = new object();
private static HttpApplication 开发者_JS百科last;
public void Init(HttpApplication context)
{
lock (initializingLock)
{
// always is false, as expected
if (last == context)
{
}
// is true for 2nd HttpApplication in AppDomain!
if (last != null && last.Application == context.Application)
{
}
last = context;
}
}
}
What's the best blace to use to store some data that's per HttpApplication that other stuff can access?
If you are coding for particular (or controlled set of) web application then you can add whatever state that you need in your HttpApplication in global.asax such as
public class Global : System.Web.HttpApplication
{
string MyProperty { get; set;}
....
Then in your module, you cast the HttpApplication to Global and access the state. For example,
var myApp = context as Global;
if (null != myApp)
{
var value = myApp.MyProperty;
...
}
精彩评论