2

I deployed an HttpModule that needs to do something in session_start and session_end events.
I think everything's just fine, because I enabled the session state and set it to "InProc" mode and defined the timeout and activate the "state service" in Central Administration and also removed the "remove session" tag from web.config in modules section. The session_start event works just fine but the problem is session_end event. Here's my HttpModule code:

private void session_Start(object sender, EventArgs e)
{
    SPHttpApplication app = MyApp;
    string ip = app.Request.UserHostAddress;
    string userName = (app.User != null) ? app.User.Identity.Name : "";

    OpsToTable(app, ip, userName);
}

private void session_End(object sender, EventArgs e)
{
    SPHttpApplication app = MyApp;
    string ip = app.Request.UserHostAddress;
    string userName = (app.User != null) ? app.User.Identity.Name : "";

    OpsToTable(app, ip, userName, TableOps.RemoveFromTable);
}

public void Dispose() { }

public void Init(HttpApplication application)
{
    SPHttpApplication app = (SPHttpApplication)application;
    MyApp = app; // adding HttpApplication to property for using in session_start and session_end events

    if (app.Modules["Session"] != null)
    {
        SessionStateModule session = app.Modules["Session"] as SessionStateModule;
        session.Start += new EventHandler(session_Start);
        session.End += new EventHandler(session_End);
    }
}

Also, how can I access the HttpApplication when i'm in the session_end or start events? I defined the property and add HttpApplication in Init event to it, maybe its because of this?

5
  • are you running a multi server farm with more than one WFE? If so, have you made changes to web.config files on all WFE servers? Commented Jul 24, 2011 at 15:34
  • 1
    i have a farm with two site collection. do i have to change all of the web.config files on all site collections on farm? for example remove the "remove session" tag from all of the site collections on the farm?if it's so, no i didn't do it.you test that by yourself? Commented Jul 25, 2011 at 9:37
  • WFE = Web Front End = physical server. If your farm consist of more than one server that runs IIS, you need to make sure your updates are pushed out to all web.config files. This is usually done with the SPWebConfigModification class. Editing web.config files should generally be avoided, also with this class since its still quite buggy, but with modules you pretty much have to... Commented Jul 25, 2011 at 11:06
  • Apart from the 'also' question at the end which we'd really prefer a new question for, I can't see what the question is here? What is the problem in Session_End?
    – Alex Angas
    Commented Jul 29, 2011 at 0:13
  • 1
    actually i ain't problem with the Session_End event since this event even not firing to make a problem for me. "Why isn't the session_end event in my HttpModule firing?" i figure it out with some other solution and i solved that. my intend was to know how much users are online and i want to do this with session_start and session_end by checking and adding user in session_start to application variable and removing user from the application variable on session_end event. thanks for the time you spend. Commented Jul 31, 2011 at 4:04

1 Answer 1

1

From the SessionStateModule.End Event on MSDN:

Though the End event is public, you can only handle it by adding an event handler in the Global.asax file. This restriction is implemented because HttpApplication instances are reused for performance. When a session expires, only the Session_OnEnd event specified in the Global.asax file is executed, to prevent code from calling an End event handler associated with an HttpApplication instance that is currently in use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.