Reputation: 879
I am fairly new to .NET and am having the following issue. I am working on an internal application that uses Window Authentication. I need to create a temporary object and store it in the database for the duration of the session. Once the session ends, I no longer need that object.
I have code in Session_start that creates the object, and code in Session_end that destroys it.
protected void Session_Start(Object sender, EventArgs e)
{
MyDbContext db = new MyDbContext ();
MyObject o = new MyObject () { Temporary = 1 };
db.MyObjects.Add(o);
db.SaveChanges();
HttpContext.Current.Session.Add("my_object_id", o.ID);
}
protected void Session_End(Object sender, EventArgs e)
{
MyDbContext db = new MyDbContext ();
try
{
MyObject o = db.MyObjects.Find(HttpContext.Current.Session["my_object_id"]);
db.MyObjects.Remove(o);
db.SaveChanges();
}
catch (Exception)
{
//do nothing
}
}
I also have the following xml entry in my config file
<sessionState mode="InProc" timeout="30" />
My issue is that these objects are being created but never destroyed. Currently we are hosting our application on a dev server, as well as running it locally with Visual Studios internal iisexpress server.
I will be trying to add some sort of logging to see if session_end is ever being called. This application will not have any sort of logout mechanism(Internal app with windows authentication).
I am not sure if I am even doing this right. Any help would be appreciated.
Upvotes: 2
Views: 883
Reputation: 101130
Make sure that the application pool timeout is larger than the session timeout. Otherwise the application pool will be recycled before the Session_End
method is invoked (if your site do not have many visitors).
You can find the setting here:
http://bradkingsley.com/iis7-application-pool-idle-time-out-settings/
Upvotes: 4