Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Sep 23, 2006 09:26 PM by lfalcao
Aug 03, 2006 02:01 PM|LINK
Aug 05, 2006 04:39 PM|LINK
just did a few google searches and it looks like it is possible to add read/write of the session object by implementing IRequiresSessionState interface
You need to implement the System.Web.SessionState.IRequiresSessionState
interface on your
IHttpHandler (note that this is a marker interface
with no methods to implement -- just add it to your class declaration).
I believe all you need to do is add IRequiresSessionState to your class declaration?
public class TestHandler: IHttpHandler, IRequiresSessionState
Aug 12, 2006 02:52 PM|LINK
Sep 20, 2006 06:54 PM|LINK
That is a really interesting question! Did anybody find a solution of how to access the Session variables inside a HttpHandlerFactory?
I tried now multiple things and the least worse one, which still does not work is this one, I think:
public IHttpHandler GetHandler(HttpContext context,
string url, string pathTranslated)
string newUrl = url;
Match match =
newUrl = this.siteRegex.Replace(url,
IHttpHandler handler =
PageParser.GetCompiledPageInstance(newUrl, context.Server.MapPath(newUrl), context);
HttpSessionState session = (handler
session["VALUE"] = match.Value;
context.Session is still null, HttpContext.Session is null and if I try to access the Session from the handler I get an exception that the session state can only be used if enableSessionState is set to true and so forth.
MyPage implements IRequiresSessionState, the property EnableSessionState is set to true and I do use the session inside the page_load and so on. I just cannot access it from the HttpHandlerFactory.
Purpose of that factory is to extract a value from the url, redirect the user to the correct page and set some values othe session that I use to fetch data, but that should not be setable via the querystring.
Sep 22, 2006 11:11 AM|LINK
Yes, I do understand what's wrong with your approach. Although it is a long story, I'll have a go and try to briefly explain it.
In the ASP.NET runtime, request processing is performed by executing a sequence of steps. One of these steps is responsible for session state establishment, meaning, associating the session state container (corresponding to the current request) with the
current HttpContext. This step is implemented by the HttpModule System.Web.SessionState.SessionStateModule.
Another request processing step performs URL-endpoint mapping, where endpoint must be: a type that implements IHttpHandlerFactory;
or a type that implements
IHttpHandler. If the specified endpoint (in web.config) is a type that implements
IHttpHandlerFactory, its GetHandler method is called immediately.
The problem is that URL-endpoint mapping is performed prior to session state establishment. This means that it is
impossible to access session state variables in the custom factory GetHandler method, simply because the ASP.NET architecture is not designed to provide session state access to the factory. This means that your approach is not valid, given
the runtime design.
In order to give you a solution, I require a better explanation of what you intend to accomplish.
Sep 23, 2006 09:26 PM|LINK
The previous post from Paulo is absolutely right and I add here some reasoning why session state is not initialized in the handler factory.
When the HttpHandlerFactory.GetHandler executes the session state is not reestablished, and could not be. For performance reasons, the framework may not initialize session state, or initialize it in read-only mode. This decision depends
on the handler. If the handler implements IRequiresSessionState, the session state is fully reestablished in read/write mode. If the handler implements IReadOnlySessionState the session state is read-only (meaning that at the end of the request the state is
not saved). If the handler implements none of these interfaces the session state is not reestablished.
In short, the decision of initializing session state depends on the handler, so it can’t be initialized before the handler is known (created).