简体   繁体   中英

Check if session is null is returning error

I'm using jQuery to get a few values and them i send to an Webservice. On that WebService i'm checking if a session is null but i'm receiving Null Reference to that session and it gets error (i see it on javascript console, on debugging i just see the error on output "A first chance exception of type 'System.NullReferenceException' occurred in PromotorDeSaude.DLL".

I'm checking if session is null like this:

if (HttpContext.Current.Session["encomenda"] != null)

Am i doing something wrong?

That suggests that HttpContext.Current or HttpContext.Current.Session may be null - which would happen if you're running on a different thread, for example.

I suggest you log every bit of the expression to find out which bit is null, and work from there.

To access Session in a web service method, add the attribute

[WebMethod(EnableSession = true)]

to the method. See http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM