ASP.NET 2.0 session availability in Global.asax?

K

kingski

I have a web site created with ASP.NET 1.1. availability





Global.asax:

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs
e)

{

SetCulture();

}



private void SetCulture()

{



string culturePref =
(string)ConfigurationSettings.AppSettings["DefaultCulture"];

if (Session["My_Session"] != null &&
Session["My_Session"].ToString().Length > 0)

culturePref =
Session["My_Session"].ToString();

Bla bla .

}





After migrating this web site to ASP.NET 2.0 in event viewer start appears
this:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Event code: 3005

Event message: An unhandled exception has occurred.

Event time: 10.05.2006 16:24:07

Event time (UTC): 10.05.2006 14:24:07

Event ID: 6d22d64bd6174ea297a527bf2953c52b

Event sequence: 27

Event occurrence: 2

Event detail code: 0



Application information:

Application domain: /LM/W3SVC/2040315736/Root-2-127917444735263147

Trust level: Full

Application Virtual Path: /

Application Path: D:\Inetpub\www.luup.com\

Machine name: VINSON



Process information:

Process ID: 5352

Process name: w3wp.exe

Account name: NT AUTHORITY\NETWORK SERVICE



Exception information:

Exception type: HttpException

Exception message: Session state is not available in this context.



Request information:

Request URL:
https://www.vinson.com/webresource.axd?d=w8xrHbl9QllPRpFUhORWyw2&t=632775007064853919

Request path: /webresource.axd

User host address: 191.128.1.222

User:

Is authenticated: False

Authentication Type:

Thread account name: NT AUTHORITY\NETWORK SERVICE



Thread information:

Thread ID: 23

Thread account name: NT AUTHORITY\NETWORK SERVICE

Is impersonating: False

Stack trace: at System.Web.HttpApplication.get_Session()

at MyComp.Web.MyCompWeb.Global.SetCulture()

at
MyComp.Web.MyCompWeb.Global.Application_PreRequestHandlerExecute(Object
sender, EventArgs e)

at
System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously)

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



What is the first place /method/ in which is possible to access session in
Global.asax?

Any ideas what other can cause this exception?
 
G

Guest

In ASP.NET 2.0, the Session object is not "Live" until something has been
added to it.
I'm not sure if its also available in PreRequestHandlerExcute either, but
this should help.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




kingski said:
I have a web site created with ASP.NET 1.1. availability





Global.asax:

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs
e)

{

SetCulture();

}



private void SetCulture()

{



string culturePref =
(string)ConfigurationSettings.AppSettings["DefaultCulture"];

if (Session["My_Session"] != null &&
Session["My_Session"].ToString().Length > 0)

culturePref =
Session["My_Session"].ToString();

Bla bla .

}





After migrating this web site to ASP.NET 2.0 in event viewer start appears
this:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Event code: 3005

Event message: An unhandled exception has occurred.

Event time: 10.05.2006 16:24:07

Event time (UTC): 10.05.2006 14:24:07

Event ID: 6d22d64bd6174ea297a527bf2953c52b

Event sequence: 27

Event occurrence: 2

Event detail code: 0



Application information:

Application domain: /LM/W3SVC/2040315736/Root-2-127917444735263147

Trust level: Full

Application Virtual Path: /

Application Path: D:\Inetpub\www.luup.com\

Machine name: VINSON



Process information:

Process ID: 5352

Process name: w3wp.exe

Account name: NT AUTHORITY\NETWORK SERVICE



Exception information:

Exception type: HttpException

Exception message: Session state is not available in this context.



Request information:

Request URL:
https://www.vinson.com/webresource.axd?d=w8xrHbl9QllPRpFUhORWyw2&t=632775007064853919

Request path: /webresource.axd

User host address: 191.128.1.222

User:

Is authenticated: False

Authentication Type:

Thread account name: NT AUTHORITY\NETWORK SERVICE



Thread information:

Thread ID: 23

Thread account name: NT AUTHORITY\NETWORK SERVICE

Is impersonating: False

Stack trace: at System.Web.HttpApplication.get_Session()

at MyComp.Web.MyCompWeb.Global.SetCulture()

at
MyComp.Web.MyCompWeb.Global.Application_PreRequestHandlerExecute(Object
sender, EventArgs e)

at
System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously)

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



What is the first place /method/ in which is possible to access session in
Global.asax?

Any ideas what other can cause this exception?
 
B

Brock Allen

Session is available in PreRequesthandlerExecute, contrary to what others
have posted.

The problem is that your code is being executed for every request into the
server, and some requests (like ones for WebResourxe.axd) don't utlilize
Session (because the handler doesn't implement IRequireSessionState). So
change your code to only access Session if that request has access to it.
Change your code to do this:

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs
e)

{

if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
SetCulture();

}





I have a web site created with ASP.NET 1.1. availability

Global.asax:

protected void Application_PreRequestHandlerExecute(Object sender,
EventArgs e)

{

SetCulture();

}

private void SetCulture()

{

string culturePref =
(string)ConfigurationSettings.AppSettings["DefaultCulture"];

if (Session["My_Session"] != null &&
Session["My_Session"].ToString().Length > 0)

culturePref =
Session["My_Session"].ToString();

Bla bla .

}

After migrating this web site to ASP.NET 2.0 in event viewer start
appears this:

----------------------------------------------------------------------
----------------------------------------------------------------------
----------------------------------------------------

Event code: 3005

Event message: An unhandled exception has occurred.

Event time: 10.05.2006 16:24:07

Event time (UTC): 10.05.2006 14:24:07

Event ID: 6d22d64bd6174ea297a527bf2953c52b

Event sequence: 27

Event occurrence: 2

Event detail code: 0

Application information:

Application domain: /LM/W3SVC/2040315736/Root-2-127917444735263147

Trust level: Full

Application Virtual Path: /

Application Path: D:\Inetpub\www.luup.com\

Machine name: VINSON

Process information:

Process ID: 5352

Process name: w3wp.exe

Account name: NT AUTHORITY\NETWORK SERVICE

Exception information:

Exception type: HttpException

Exception message: Session state is not available in this context.

Request information:

Request URL:
https://www.vinson.com/webresource.axd?d=w8xrHbl9QllPRpFUhORWyw2&t=632
775007064853919

Request path: /webresource.axd

User host address: 191.128.1.222

User:

Is authenticated: False

Authentication Type:

Thread account name: NT AUTHORITY\NETWORK SERVICE

Thread information:

Thread ID: 23

Thread account name: NT AUTHORITY\NETWORK SERVICE

Is impersonating: False

Stack trace: at System.Web.HttpApplication.get_Session()

at MyComp.Web.MyCompWeb.Global.SetCulture()

at
MyComp.Web.MyCompWeb.Global.Application_PreRequestHandlerExecute(Objec
t sender, EventArgs e)

at
System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpAppli
cation.IExecutionStep.Execute()

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
Boolean& completedSynchronously)

----------------------------------------------------------------------
----------------------------------------------------------------------
----------------------------------------------------

What is the first place /method/ in which is possible to access
session in Global.asax?

Any ideas what other can cause this exception?
 
Joined
Oct 10, 2008
Messages
2
Reaction score
0
if (System.Web.HttpContext.Current.Session != null)

Hi;

You must try with this change:

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs
e)
{
if (System.Web.HttpContext.Current.Session != null)
SetCulture();
}



private void SetCulture()
{
string culturePref =
(string)ConfigurationSettings.AppSettings["DefaultCulture"];

if (Session["My_Session"] != null &&
Session["My_Session"].ToString().Length > 0)

culturePref =
Session["My_Session"].ToString();

Bla bla .
}

Best Regards
Ivan Fernando Sarachman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top