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
Thanks for the indication. I understand how IRequiresSessionState can be used within a IHttpHandler to get access to the Session state. But this point does not answer how the Session can be accessed within an IHttpHandlerFactory. Does it?
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 requestType,
string url, string pathTranslated)
{
string newUrl = url;
Match match =
this.regex.Match(url);
if (match.Success)
newUrl = this.siteRegex.Replace(url,
"/");
IHttpHandler handler =
PageParser.GetCompiledPageInstance(newUrl, context.Server.MapPath(newUrl), context);
if (match.Success)
{
if (handler
is MyPage)
{
HttpSessionState session = (handler
as MyPage).Session;
session["VALUE"] = match.Value;
}
}
return handler;
}
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.
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.
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).
Joannes Verm...
Participant
909 Points
213 Posts
HTTP Handler factory: the HttpContext argument is not initialized?
Aug 03, 2006 02:01 PM|LINK
Yet, I would really need to get access to the Session state for the IHttpHandlerFactory. Does anyone understand what's wrong with my approach?
Thanks in advance,
Joannes
http://www.peoplewords.com
jhouse
Participant
1856 Points
374 Posts
Re: HTTP Handler factory: the HttpContext argument is not initialized?
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
see http://dotnet247.com/247reference/msgs/13/67881.aspx
I believe all you need to do is add IRequiresSessionState to your class declaration?
public class TestHandler: IHttpHandler, IRequiresSessionState
Joannes Verm...
Participant
909 Points
213 Posts
Re: HTTP Handler factory: the HttpContext argument is not initialized?
Aug 12, 2006 02:52 PM|LINK
John.Doe
Member
430 Points
176 Posts
Re: HTTP Handler factory: the HttpContext argument is not initialized?
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 requestType, string url, string pathTranslated)
{
string newUrl = url;
Match match = this.regex.Match(url);
if (match.Success)
newUrl = this.siteRegex.Replace(url, "/");
IHttpHandler handler = PageParser.GetCompiledPageInstance(newUrl, context.Server.MapPath(newUrl), context);
if (match.Success)
{
if (handler is MyPage)
{
HttpSessionState session = (handler as MyPage).Session;
session["VALUE"] = match.Value;
}
}
return handler;
}
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.
Paulo Pereir...
Member
15 Points
3 Posts
Re: HTTP Handler factory: the HttpContext argument is not initialized?
Sep 22, 2006 11:11 AM|LINK
Hello Joannes.
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.
Paulo
lfalcao
Member
32 Points
7 Posts
Re: HTTP Handler factory: the HttpContext argument is not initialized?
Sep 23, 2006 09:26 PM|LINK
Hi.
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).
HttpHandlerFactory
-- Luis Falcão