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
<compilation numRecompilesBeforeAppRestart=/> setting in machine.config or web.config
(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/
======================================
"Scott Roberts" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "gnewsgroup" <(E-Mail Removed)> wrote in message
> news:f0e3dc16-36ed-4733-8491-(E-Mail Removed)...
>> 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.
>>
>
> 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.