.NET Forum / ASP.NET / General / March 2008
session variable getting lost
|
|
Thread rating:  |
SAL - 06 Nov 2007 22:15 GMT Okay, don't bash me to hard for my design on this app, it's my first web app and it's in production. My basic design is using Datatables created via the designer with a business logic class in between the datatable and ObjectDataSources. In one page I had a Gridview with select enabled. When an row in the grid is selected, I retrieve the SelectedValue, store the value in a Session variable and redirect the response to another web page, which, has an ObjectDataSource on it and a DetailsView. The ObjectDataSource on this page retrieves the value of the Session variable set from the first page. The problem is that the Session variable is randomly losing its value. So, the ObjectDataSource's Business object's SelectMethod accepts a value to select records by. How do I know that the value is getting lost? Because no rows are returned as: dr = dtAdapter.Rows(0)
throws an exception IndexOutOfRange.
My question is, is there some reason that my session variable evaporates randomly?
Thanks in advance
SAL
Mark Rae [MVP] - 06 Nov 2007 22:28 GMT > My question is, is there some reason that my session variable evaporates > randomly? No. Session variables will be lost when the Session times out, or when they are overwritten / removed - does either of these situations apply here...?
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
SAL - 06 Nov 2007 23:16 GMT Mark, thanks for the reply. I do not see either of these things applying here. This happened twice this morning for no apparent reason and then it didn't happen. I have not changed the time out so it should still be 20 minutes and I am not overwriting them as far as I can see. I can not reproduce it on my machine.
S
>> My question is, is there some reason that my session variable evaporates >> randomly? > > No. Session variables will be lost when the Session times out, or when > they are overwritten / removed - does either of these situations apply > here...? Peter Bromberg [C# MVP] - 07 Nov 2007 01:21 GMT Sal, The only time a Session item would be lost under "normal circumstances" (e.g. it hasn't timed out by itself due to no page requests) is that IIS is recycling the application. Recycling can be because of IIS settings, because something touched one or more of the files in the web folder "tree", or because you've got buggy code that's causing unhandled exceptions. In any of these cases, you can bet that your Application, Session and Cache items will all go down the potty. Peter
 Signature http://www.eggheadcafe.com unBlog: http://petesbloggerama.blogspot.com BlogMetaFinder: http://www.blogmetafinder.com
> Mark, thanks for the reply. > I do not see either of these things applying here. This happened twice this [quoted text clipped - 10 lines] > > they are overwritten / removed - does either of these situations apply > > here...? SAL - 07 Nov 2007 21:13 GMT Okay, so let's say that all of the things you've listed below are not happening. But, that a Response.Redirect may be. The documentation states that Redirect calls End which raises a ThreadAbortException. I do not see this exception however. I am wondering if this could be at the root of my random problem here...
Thanks for your responses S
> Sal, > The only time a Session item would be lost under "normal circumstances" [quoted text clipped - 27 lines] >> > they are overwritten / removed - does either of these situations apply >> > here...? George Ter-Saakov - 07 Nov 2007 21:34 GMT Here are some guesses why session might be lost.
1. Host must be the same. Example http://www.mysite.com and http://mysite.com might be 2 different sites. Thus browser does not pass cookies from one to another. Thus session might be lost. If for example you logged in into http://mysite.com/login.aspx and then got redirected to http://www.mysite.com
2. Basically comes from #1. http://www.mysite.com and http://132.234.123.123 are 2 different sites even if www.mysite.com resolves to 132.234.123.123
3. Application is recycled. To verify #3 add folowing code to Global.asax You need to replace last line clsGlobal.SendEmail with your emailing code. This will send an email to you with a reason everytime application was shutdown. Thus you will know.....Unless of course someone pulls a power plug from the server. Then no email will be sent :)
protected void Application_End(Object sender, EventArgs e)
{
HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
if (runtime == null)
return;
string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
string shutDownStack = (string)runtime.GetType().InvokeMember("_shutDownStack",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
//send email to me
clsGlobal.SendEmail(String.Format("\r\n\r\n_shutDownMessage={0}\r\n\r\n_shutDownStack={1}",
shutDownMessage,shutDownStack), "Application Shutdown", youremail@youremail.com, null, null);
}
George
> Okay, so let's say that all of the things you've listed below are not > happening. But, that a Response.Redirect may be. The documentation states [quoted text clipped - 36 lines] >>> > they are overwritten / removed - does either of these situations apply >>> > here...? SAL - 08 Nov 2007 17:23 GMT George, thanks for you suggestion. I have implemented the Application_End routine in Global.aspx and the application is getting shut down with the following information. If you can help it would be great:
HostingEnvironment caused shutdown
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo) at System.Environment.get_StackTrace() at System.Web.HttpRuntime.ShutdownAppDomain() at System.Web.Hosting.HostingEnvironment.ShutdownThisAppDomainOnce() at System.Web.Hosting.HostingEnvironment.InitiateShutdownWorkItemCallback(Object state) at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
S
> Here are some guesses why session might be lost. > [quoted text clipped - 94 lines] >>>> > apply >>>> > here...? Walter Wang [MSFT] - 09 Nov 2007 03:54 GMT Hi SAL,
This looks like a normal shutdown, try to use following method to log the cause and the time it occurs:
#Logging ASP.NET Application Shutdown Events - ScottGu's Blog http://weblogs.asp.net/scottgu/archive/2005/12/14/433194.aspx
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
SAL - 14 Nov 2007 16:52 GMT Okay, I did that. Now I'm getting this in the event log:
The description for Event ID ( 0 ) in Source ( .NET Runtime ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event:
_shutDownMessage=HostingEnvironment caused shutdown
_shutDownStack= at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo) at System.Environment.get_StackTrace() at System.Web.HttpRuntime.ShutdownAppDomain() at System.Web.Hosting.HostingEnvironment.ShutdownThisAppDomainOnce() at System.Web.Hosting.HostingEnvironment.InitiateShutdownWorkItemCallback(Object state) at System.Threading.
Which I really don't understand. Help?
S
> Hi SAL, > [quoted text clipped - 15 lines] > This posting is provided "AS IS" with no warranties, and confers no > rights. Walter Wang [MSFT] - 14 Nov 2007 03:46 GMT Hi SAL,
How's the issue now?
Not directly related, but I think following information might be useful:
#Thomas Marquardt's Blog : ASP.NET File Change Notifications, exactly which files and directories are monitored? http://blogs.msdn.com/tmarq/archive/2007/11/02/asp-net-file-change-notificat ions-exactly-which-files-and-directories-are-monitored.aspx
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
SAL - 14 Nov 2007 16:57 GMT Hi Walter. It's just as bad as ever and what I don't get is that, I'm not changing anything when this stuff occurs that I'm aware of. All I'm doing is redirecting to another page, changing DetailsView states to edit, cancelling the edit, clicking another menu item and doing the same thing all over again and it will restart which causes the ID of the item the viewer is using to be lost in the session variable. And, since the ObjectDataSource is getting records based on this ID, the app fails with a web page that isn't showing any data.
S
> Hi SAL, > [quoted text clipped - 19 lines] > This posting is provided "AS IS" with no warranties, and confers no > rights. Walter Wang [MSFT] - 07 Nov 2007 03:23 GMT Hi SAL,
Based on my understanding, you're redirecting to the second page right after setting the session variable, right? If this is the case, there's no reason the session variable will be lost due to session timeout. Could you please double check if the session variable is really lost or it's because the query that uses the variable to retrieve records from database returns empty result?
By the way, there's another way to pass variables between pages using Context.Items and Server.Transfer:
#Famil Jones : Pass Variables Between ASP.NET Pages http://www.dotnetjunkies.com/WebLog/familjones/archive/2004/04/08/11020.aspx
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
SAL - 07 Nov 2007 18:17 GMT Hmmm, thank you for the replies all. Would it be possible for Walter or someone else to comment on the load that using the Server.Transfer pattern would place on the server? So, is there an increased load on the server using this model as compared to using Session variables?
Thanks SAL
> Mark, thanks for the reply. > I do not see either of these things applying here. This happened twice [quoted text clipped - 11 lines] >> they are overwritten / removed - does either of these situations apply >> here...? Walter Wang [MSFT] - 08 Nov 2007 09:11 GMT Hi SAL,
Regarding difference between Response.Redirect and Server.Transfer, I think following page is useful for your reference. Each approach has its own pros and cons.
#Response.Redirect vs Server.Transfer http://haacked.com/archive/2004/10/06/responseredirectverseservertransfer.as px
Back to the original issue about losing session after Redirect, I think George's suggestion is quite helpful.
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
SAL - 08 Nov 2007 16:39 GMT Thank you for your reply and the article link. I am currently investigating George's suggestion as well
S
> Hi SAL, > [quoted text clipped - 22 lines] > This posting is provided "AS IS" with no warranties, and confers no > rights. kpg* - 08 Nov 2007 18:26 GMT > Thank you for your reply and the article link. > I am currently investigating George's suggestion as well Please post back here if (and when) you find the culprit.
I have stopped using sesion variables altogether becuase of similar issues on a client's server, but am very interested in what you figure out.
Now my client may well be changing the web.config file which will cause the application to restart, all the more reason to not use session varialbes (in my case).
Instead I pass info encrypted in the query string, which works just fine, the only session variable I use is to relay error messages to tan error page - if that gets lost, oh well.
kpg
SAL - 15 Nov 2007 19:12 GMT kpg, what I figured out was the app was getting recycled due to a limit I had set on virtual memory. There are times when the virtual memory usage jumps up dramatically and then it pops right back down. The app does not appear to be leaking memory at this time. So, I unchecked that option in the App pool's configuration. I used information garnered from this link to aid me in my quest, although I haven't completely implemented Peter's Exception Engine. http://www.eggheadcafe.com/articles/20030816.asp
I placed this code in my Application_Error routine in my Global.asax file:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when an unhandled error occurs ' check to see if the ASPNETApplication log exists Dim b As Boolean Try b = Convert.ToBoolean(ConfigurationManager.AppSettings("logAppErrors")) Catch ex As Exception
End Try If Not b Then Exit Sub
If Not System.Diagnostics.EventLog. _ SourceExists("AnnexationTrackerApplication") Then System.Diagnostics.EventLog. _ CreateEventSource("AnnexationTrackerApplication", "Application") End If System.Diagnostics.EventLog. _ WriteEntry("AnnexationTrackerApplication", Server.GetLastError().GetBaseException().Message)
End Sub
The message appeared in the system event log not the application event log.
HTH S
>> Thank you for your reply and the article link. >> I am currently investigating George's suggestion as well [quoted text clipped - 15 lines] > > kpg Carlos Vasquez - 28 Mar 2008 13:13 GMT There is a problem with losing values in Session Variable. I am currently troubleshooting as to why. I am working with tabs and all code is on the same page and my session variables are dropping. Microsoft acknowledge this is a problem: http://support.microsoft.com/kb/316148. I am still trying to figure out why I am losing my session variables. None of the issues mentioned here or on microsoft have been found to be the culprit.
If anyone knows why, I would like to know.
Juan T. Llibre - 28 Mar 2008 13:28 GMT That KB is over 2 years old.
Antivirus scans are but one of the possible reasons for applications to restart, which in turn causes the loss of session variables if session state is kept in-process.
Modifying the source code of your Web application will cause ASP.NET to recompile source files into assemblies.
When you modify the top-level items in your application, all other assemblies in the application that reference the top-level assemblies are recompiled as well.
In addition, modifying, adding, or deleting certain types of files within the application's known folders will cause the application to restart.
The following actions will cause an application restart:
1. Adding, modifying, or deleting assemblies from the application's Bin folder.
2. Adding, modifying, or deleting localization resources from the App_GlobalResources or App_LocalResources folders.
3. Adding, modifying, or deleting the application's Global.asax file.
4. Adding, modifying, or deleting source code files in the App_Code directory.
5. Adding, modifying, or deleting Profile configuration.
6. Adding, modifying, or deleting Web service references in the App_WebReferences directory.
7. Adding, modifying, or deleting the application's Web.config file.
Juan T. Llibre, asp.net MVP asp.net faq : http://asp.net.do/faq/ foros de asp.net, en español : http://asp.net.do/foros/ ======================================
> There is a problem with losing values in Session Variable. I am > currently troubleshooting as to why. I am working with tabs and all [quoted text clipped - 7 lines] > > *** Sent via Developersdex http://www.developersdex.com ***
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|