locked
HttpModule, PreRequestHandler and Webservice throws NullReferenceException RRS feed

  • Question

  • User-32825938 posted
    Hi all!

    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);
            }
    ------------------------------------------------------------------------------
    Thursday, February 9, 2006 12:50 PM

All replies

  • User2133169861 posted
    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.
    Monday, February 20, 2006 9:52 AM