I'm using a HttpModule plugged into my site to handle the PreRequestHandlerExecute to set Culture based on session data or browser settings. This works perfectly fine...until I try to implement a webservice on this particular site. (The webservice works fine
when unplugging the module).
What happens is that a NullReferenceException is thrown, and the real frustrating part is that I can't figure out where this is coming from and where to check for nulls.
It seems that when using a HttpModule with the PreRequestHandlerExecute throws a NullReferenceException when called via a webservice.
The problem is that by default session state is not enabled for web methods, so context.Session is null, which will throw an exception when you try to call the indexer method. To enable session state add EnableSession=true to your WebMethod attribute. Also,
you don't need to write a HttpModule just to subscribe to the PreRequestHandlerExecute event. You can do that from Global.asax.
None
0 Points
10 Posts
HttpModule, PreRequestHandler and Webservice throws NullReferenceException
Feb 09, 2006 12:50 PM|daniel.andersson|LINK
I'm using a HttpModule plugged into my site to handle the PreRequestHandlerExecute to set Culture based on session data or browser settings. This works perfectly fine...until I try to implement a webservice on this particular site. (The webservice works fine when unplugging the module).
What happens is that a NullReferenceException is thrown, and the real frustrating part is that I can't figure out where this is coming from and where to check for nulls.
It seems that when using a HttpModule with the PreRequestHandlerExecute throws a NullReferenceException when called via a webservice.
Any ideas are greatly appreciated.
My code:
------------------------------------------------------------------------------
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
// Kolla om culture är satt via webbgränssnittet
string lang = context.Session["culture"] == null ? null : context.Session["culture"].ToString();
// Annars hämtas det från webbläsaren
if (lang == null)
lang = context.Request.UserLanguages[0];
// Sätt culture
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
}
------------------------------------------------------------------------------
Member
200 Points
173 Posts
Re: HttpModule, PreRequestHandler and Webservice throws NullReferenceException
Feb 20, 2006 09:52 AM|smalltalk|LINK
http://ionut.bizau.ro