开发者

Session state is not available in this context

I want to read session id in application error event but always g开发者_Python百科et error "Session state is not available in this context". Why? The strange thing is that I have the same code in another asp.net app and everything works fine.

void Application_Error(object sender, EventArgs e)
{

        var sessionId = Session.SessionID;
        //skipped code

}


The session object may not be available this is dependent on when the error occured.

For example if an error occured on Begin_Request the session would not be available as it has not yet been created.

So in summary sometimes it will work sometimes not, depending on when the error occured.

Best to check the state of the session object before accesssing the session id e.g.

HttpContext context = HttpContext.Current;

if (context != null && context.Session != null) ...


check if any event missing in c# which is mapped to a control or issue in design part


Application_error can fire in situations where a session is not present, for example when the garbage collector cleans up. The source of the error may not have been a user thread.

Just check whether session is null first.

Simon


You may get this error if you are using an Out-of-Process state mode for ASP.NET state server. You need to mark any classes you wish to save to session state with the [Serializable] attribute

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜