Why does it show a NullReferenceException when the variable is not null?

Nre

Why does VS 2012 show the Type variable as a NullReferenceException, when it also shows that it is value = "Retailer" .

enter image description here

I have a newborn and am working on limited sleep, so I apologize if I missed something obvious here. The LoggedInUser.Employer object was created, and this line works perfectly one and a half times. But then it starts to break. Not sure if this helps - need to sleep ...

  private string _type; public string Type { get { return _type; } set { if (value != null) { TypeEnum = (Constants.BusinessType)Enum.Parse(typeof(Constants.BusinessType), value, true); _type = value; } } } 

enter image description here I'm starting to wonder if there is a problem with cross-threads ...

enter image description here

+7
source share
3 answers

The ASP.NET ExecutionContext , which is responsible for storing the HttpContext.Current instance, will naturally not spill over into other threads. Judging by your error stack trace, you are running ASP.NET MVC, which abstracts the use of HttpContext . Perhaps you came from the background of WebForms, where its direct use is common?

SynchronizationContext

This article offers much more detail than I can reasonably delve into. Some of the most important questions for your situation:

"ExecutionContext is environmental information, that is, it stores data related to the current environment or" context "in which you work.

This "surrounding" information is ... HttpContext.Current and its various properties (including Session ).

"This means that this surrounding context, which we use to monitor the details of our execution, is no longer viable because TLS does not" flow "through these asynchronous points.

TLS is a stream-local storage ( HttpContext.Current , etc.). In short, async = potentially loses HttpContext.Current .

MVC path

Remember, I said that MVC basically abstracts from HttpContext ?

Session is located in Controller.Session . (I apologize that so far I have not tested this in the action of the asynchronous mode controller, so I cannot yet verify its suitability for your needs or you will need additional work to make it cooperate.)

Request is in Controller.Request

User is in Controller.User

There are others ... check them out .

Session alternatives?

Have you considered alternatives? You don't have to go far to find articles suggesting that Session + ASP.NET MVC is a bad idea. I will not weigh something generalized as β€œbad”, but looking at your example, it seems to me that you are dealing with user profile data, not session data.

A session is not really a good place to cache user profile information. In this regard, is it appropriate to cache it at all? Can a user profile change during a session? If they themselves changed it, would you have a reset session? What should I do if an individual admin user changes their profile during login?

Exploring alternatives is beyond the scope of this question, but just be careful that you can solve the wrong problem here.

+2
source

String.IsNullOrEmpty will not throw this exception in the string, even if it is null, so the Type property is not a problem. LoggedInUser uses 2 lines before the error, so it leaves the Employer property as the culprit (if String is not an inline string).

You can add confirmation to it to confirm:

 if (LoggedInUser.Employer != null) { if (String.IsNullOrEmpty(LoggedInUser.Employer.Type)) { ... } } else { // debug output } 

Assuming the employer is null, you need to specify this property definition. Since you only see this when multiple users are logged in, I suspect there should not be a static ad somewhere.

+1
source

My guess is that you somewhere declared a field called "String" - try using the string string "string.IsNullOrEmpty" or "System.String.IsNullOrEmpty" for one ....

0
source