Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trigger Session Start (Global.asax) Event for a WebHandler Request?

Tags:

c#

asp.net

I have a Webhandler which generates an image on request in my asp.net Project. But if the user directly access the resource, it won't trigger the session start Event in the Global.asax file. But in my project I need to trigger the session start event. How can I achieve this?

 void Session_Start(object sender, EventArgs e) 
    {
        Session["Test"] = 1;

    }
like image 417
Subin Jacob Avatar asked Apr 22 '13 06:04

Subin Jacob


2 Answers

The Session_Start event is trigerred whenever some server side handler attempts to either read or write to the session. You might try decorating your handler with the IRequiresSessionState marker interface:

public class MyHandler: IHttpHandler, IRequiresSessionState
{
    ...
}
like image 112
Darin Dimitrov Avatar answered Oct 21 '22 08:10

Darin Dimitrov


You can always make a method of the Session_Start and call it

namespace WebFormsApplication1
{
    public class Global : HttpApplication
    {
        void Session_Start(object sender, EventArgs e) 
        {
            Global.StartSession();
        }
    }

    public static class Global 
    {
        public static void StartSession() {

            Session["Test"] = 1;
        }
    }
}

and in your Handler, simply call Global.StartSession();

like image 20
balexandre Avatar answered Oct 21 '22 09:10

balexandre