.NET Framework - What causes session to disappear?

Asked By gnewsgroup
19-Jan-08 12:49 PM
In my asp.net 2.0 web application, in Web.config, I have

timeout="30", s l i d i n g E x p i r a t i o n = "true"

for the Authentication element.  I suppose, this means that as long as
we don't let a 30 min elapse without making any request, the session
should stay alive, right?

But I notice that quite often, the application throws out Null
reference exception where I try to access a session variable like so:

System.NullReferenceException: Object reference not set to an instance
of an object.

Apparently, the session variable is lost.

I am sure it is way before a 30 minutes' elapse without making any
request.

Question:

1. Do we lose the session if the project gets JIT-recompiled?

2. Do we lose the session if an Exception happens?

Thank you.
SQL Server
(1)
NullReferenceException
(1)
App Pool
(1)
IIS
(1)
Session.Contents
(1)
Machine.Config
(1)
Application
(1)
ASPInsider
(1)
  Steve C. Orr [MCSD, MVP, CSM, ASP Insider] replied...
18-Jan-08 05:28 PM
Anytime the IIS application is restarted, all sessions are restarted too.
So yes, this would generally happen during recompilations and some major
unhandled application level exceptions.

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
http://iPhonePlaza.net
  Scott Roberts replied...
18-Jan-08 06:01 PM
I think the answer depends on the session-state "Mode" of your web app.
Assuming it's "in-proc" (the default) then modifying web.config or a DLL in
the "bin" directory will do it. As will restarting the application pool
(either manually, or automatically due to memory limits, cpu utilization,
etc.).

You might try switching to StateServer or SQLServer mode, but you will need
to ensure that you only put value types and serializable objects into the
session if you do that.

In general, you should always verify the existence of a session object
before trying to use it - and handle the case where it doesn't exist.
  Juan T. Llibre replied...
18-Jan-08 07:36 PM
An application domain will unload (causing loss of session variables
unless session state is maintained with State Server or SQL Server),
when any one of the following occurs:

a. Machine.Config, Web.Config or Global.asax are modified
b. The bin directory or any of its contents is/are modified
c. The App_Code directory contents changes
d. The number of re-compilations (aspx, ascx or asax) exceeds the limit specified by the
(by default this is set to 15)
e. The physical path of the virtual directory is modified
f. The CAS policy is modified
g. A web service is restarted
h. (2.0 only) Application Sub-Directories are deleted
i. Any of a number of configurable App Pool recycling reasons occurs

This should cover most scenarios.



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/
======================================
  zhaoj replied...
19-Jan-08 12:50 PM
On Jan 18, 5:28 pm, "Steve C. Orr [MCSD, MVP, CSM, ASP Insider]"

First of all, Steve, thank you so much, and I just found your
marvelous ExportPanel yesterday and have put it into my code. Really
nice and neat.  Thanks for sharing your wonderful work for free.

Now back to this thread topic.  I also notice that the session does
not always disappear when an exception happens.  Is it possible to
predict what type of exception will cause the session to disappear?
  gnewsgroup replied...
19-Jan-08 12:50 PM
On Jan 18, 5:28 pm, "Steve C. Orr [MCSD, MVP, CSM, ASP Insider]"

Is it possible to predict what type of exception will cause the
session to disappear?  Or does M$ has a list of Exception types that
will cause session to disappear?

BTW, I found your cool ExportPanel just yesterday and have put it into
my web application.  Really nice and neat.  Thanks for sharing your
wonderful work for free.
  gnewsgroup replied...
19-Jan-08 12:50 PM
On Jan 18, 6:01 pm, "Scott Roberts" <srobe...@no.spam.here-webworks-

That brings up a question which I have always been wondering.  So you
suggest that we should always do something like:

if ( Session.Contents["MySessionVariable"]  != null)
{
// Do things with MySessionVariable
}
else
{
// Do something else, maybe including logout a user if some
critical
// session variables such as "UserName" is missing.
}

before we use that session variable?  That's a lot of if-else's if we
use quite many session variables.
  gnewsgroup replied...
19-Jan-08 12:50 PM
On Jan 18, 7:36 pm, "Juan T. Llibre" <nomailrepl...@nowhere.com>
ecified by the
or web.config
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D"Scott Roberts" <srobe...@no.spam.=

Fantastic, I never know about this.  Your list of situations are
certainly enough to explain why my sessions are mysteriously missing.
Thank you.
Create New Account
help
i m mohsin i m making a gui application using c# at front end and sql server 2005 at backend. . . . . . i made a table in sql server 2005 with 4 columns and in gui i used 4 text boxes and 4labels and cITY_HOSPITALDataSet.Tables["SERVICESMAINTENANCE"].Rows.Find(code); dr.Delete(); sERVICESMAINTENANCETableAdapter.Update(cITY_HOSPITALDataSet); } } } } C# Discussions HOSPITALDataSet (1) NullReferenceException (1) EventArgs (1) DataTable (1) DataRow (1) Tables (1) NewRow (1) TextChanged (1) What errors you give us the error message we might be able to help. Can I guess "NullReferenceException", "Object reference not set to an instance of an object."? In which case either the respectively. . . e as per your suggestion i insert two rows directly into that table using sql server 2005. . . but now the error is in the line " ctr = 3D Convert.ToInt32(codeval); " and
public class EHClass { static void Main() { try { Console.WriteLine("Executing the try statement."); throw new NullReferenceException(); } catch (NullReferenceException e) { Console.WriteLine("{0} Caught exception #1.", e); } catch { Console.WriteLine("Caught exception #2."); } finally demons in Gehenna from one room to another for all eternity and beyond. C# Discussions SQL Server (1) ArgumentOutOfRangeException (1) DirectoryNotFoundException (1) FileNotFoundException (1) PathTooLongException (1) OutOfMemoryException (1) InvalidOperationException (1) NullReferenceException (1) That's correct, and that's the whole point! E.g. supposing at the is needed. The only time I have implemeted a try / catch / finally is on a SQL connection using the USING statement to make sure the connection was checked to be closed didn't say catch every exception. If one happens to be in code dealing with SQL Server on a try / catch, then why should one be looking for anything else? Oh there
Error when open the connection between .net 2005 ans sql server 2000 ????? .NET Framework An unhandled exception of type 'System.NullReferenceException' occurred in system.data.dll Additional information: Object reference not set to an instance of languages-vb.htm Posted via DevelopmentNow.com Groups http: / / www.developmentnow.com VB.NET Discussions NullReferenceException (1) Moorman (1) Kerry (1) M. Diab, Can you show us the code you are open a connection? Kerry Moorman keywords: Error, when, open, the, connection, between, .net, 2005, ans, sql, server, 2000, ????? description: An unhandled exception of type 'System.NullReferenceException' occurred in system.data.dll Additional information: Object reference not set to an instanc
s Update() method as it throws an exception. I am using VC++ (VisualStudio 2005 and SQL Server 2005) for the interface. Thanks for any information. Manjree ADO.NET Discussions UAEHKABUtagRECT (1) SQL Server 2005 (1) Visual Studio 2005 (1) Visual Studio (1) NullReferenceException (1) StreamReader.ReadLine (1) ForeignKeyConstraint (1) DataTableCollection (1) By default, Fill simply reexecutes the query to get more information on my latest book: Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition) and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook) - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Thanks for your reply. I I am using dataset to
Windows client to consume a web service throws this error: System.Web.Services.Protocols.SoapException: Server was unable to process request. - --> System.NullReferenceException. . . . .etc I'm using Visual Studio 2008 and my VB code for the WS (which is connected to a SQL Server 2005 Express) is: Public Sub GetFee(ByVal brokerName As String, ByRef brokerFee As Single) Dim so, how can I resolve it? Thanks for any suggestions. ASP.NET Web Services Discussions SQL Server (1) NullReferenceException (1) SqlDbType (1) ParameterDirection (1) ExecuteNonQuery (1) ConnectionState (1) SoapException (1) SqlCommand (1) Did you Windows client to consume a web service throws this error: System.Web.Services.Protocols.SoapException: Server was unable to process request. -