Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

c# - IRequiresSessionState - how do I use it?

I need to be able to change when I can see session state. I found out about the IRequiresSessionState Marker Interface, but have not been able to figure out how to use it. I think I may be missing something obvious. Can one of you C# guru's give me a quick 1-2-3 step through (e.g)?

  1. Create a new class,
  2. Put public interface IRequiresSessionState in it.
  3. Use IRequiresSessionState('abra_cadabra') to change the value.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. To mark a class add a colon to the existing class name and put the marker. If I had a class: public class PageTools Marked, it would look like public class PageTools : IRequiresSessionState or with both interfaces, public class PageTools : IRequiresSessionState, IHttpHandler. In my case, my class needed only to be marked with the first. My handler needed both.
  2. If you right click on the marker you just typed in, you can choose implement from the menu (visual studio) and the necessary methods will be added to your class. Or you can look them up and add them manually.
  3. Once you have the IRequiresSessionState marker you can test to see if the session state is readonly and if so set a new http handler.

    if (context.Handler is IReadOnlySessionState
        || context.Handler is IRequiresSessionState)
    {
        context.Handler = Handler();
    }
    
  4. The http handler: MSDN will tell you a lot about HttpHandlers and HttpModules. In my case I needed a dummy handler so that I could access the session state when it was normally read only (Page_PreInit). So in my class I added this:

    protected IHttpHandler Handler()
    {
        MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler
            as MyHttpHandler;
        if (resourceHttpHandler != null) // set the original handler back 
        {                
            return resourceHttpHandler.OriginalHandler;
        }
        // at this point session state should be available      
        return HttpContext.Current.Handler;
    }
    
    public class MyHttpHandler : IHttpHandler, IRequiresSessionState
    {
        internal readonly IHttpHandler OriginalHandler;
    
        public MyHttpHandler(IHttpHandler originalHandler)
        {
            OriginalHandler = originalHandler;
        }
        public void ProcessRequest(HttpContext context)
        {
            // do not worry, ProcessRequest() will not be called,
            // but let's be safe         
            throw new InvalidOperationException(
                "MyHttpHandler cannot process requests.");
        }
        public bool IsReusable
        {
            // IsReusable must be set to false since class has a member!
            get { return false; }
        }
    }
    

Here is a reference to a very elegant HttpModule class from which I got much or what I used. I hope this helps someone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...