c# - How to execute server side code just before the asp.net session variable expires? -


in asp.net website creating session upon user login , perform operations in database before session expire.i having problem in determining should write code , how know session going expire.

i not sure if 'session_end' event of 'global.asax' suits requirements session want check created manually(not browser instance).

could please put me in right direction?

thanks.

this pretty tricky, namely because session_end method supported when session mode set inproc. do, use ihttpmodule monitors item stored in session, , fires event when session expires. there example on over codeproject (http://www.codeproject.com/kb/aspnet/sessionendstatepersister.aspx), not without limitations, instance doesn't work in webfarm scenarios.

using munsifali's technique, do:

<httpmodules>  <add name="sessionendmodule" type="sessiontestwebapp.components.sessionendmodule, sessiontestwebapp"/> </httpmodules> 

and wire module @ application start:

protected void application_start(object sender, eventargs e) {   // in our sample application, want use value of session["useremail"] when our session ends   sessionendmodule.sessionobjectkey = "useremail";    // wire static 'sessionend' event handler   sessionendmodule.sessionend += new sessionendeventhandler(sessiontimoutmodule_sessionend); }  private static void sessiontimoutmodule_sessionend(object sender, sessionendedeventargs e) {    debug.writeline("sessiontimoutmodule_sessionend : sessionid : " + e.sessionid);     // value in session key specified in application_start    // in demonstration, we've set 'useremail', value of session["useremail"]    object sessionobject = e.sessionobject;     string val = (sessionobject == null) ? "[null]" : sessionobject.tostring();    debug.writeline("returned value: " + val); } 

then, when session starts, can throw in user data:

protected void session_start(object sender, eventargs e) {    debug.writeline("session started: " + session.sessionid);     session["userid"] = new random().next(1, 100);    session["useremail"] = new random().next(100, 1000).tostring() + "@domain.com";     debug.writeline("userid: " + session["userid"].tostring() + ", useremail: " +                   session["useremail"].tostring()); } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -