开发者

ASP.NET HttpApplication local storage

开发者 https://www.devze.com 2023-01-13 18:41 出处:网络
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.

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;
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消