3

I am getting NullReferenceExceptions on a webforms project I'm maintaining. The catch is that there is no stacktrace for this exception because none of my code causes the exception. NullReferenceException was unhandled

Exception details copied to the clipboard offers zero assistance:
System.NullReferenceException was unhandled Message: An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll Additional information: Object reference not set to an instance of an object.

When I view the non-user code stacktrace, I see the following (all in mscorlib): Stack Trace

The error occurs randomly and inconsistently, either by loading pages or by postbacks. The problem began after I added System.Net.Http.HttpClient for pulling data from REST services exposed on other sites. Note that HttpClient contains only async methods for sending/receiving data. Based on the internal stacktrace, I highly suspect the Task<> / async / await as the culprit.

To assist in troubleshooting, let me reaffirm that I'm running this in a WebForms site compiling in .NET 4.6 (and if you're about to tell me my problem is that I need to upgrade to MVC, save your keystrokes and don't say it). While HttpClient exposes everything as Task<>, I am calling them synchronously by calling:
Task<MyObject> myResultTask = restClient.GetResultAsync(); MyObject myResult = myResultTask.Result; // no await

Thanks in advance!

8
  • 3
    just call await lol Commented Aug 18, 2016 at 15:21
  • 2
    @RenéVogt if I remember right you can easily run into all kinds of weird problems without await Commented Aug 18, 2016 at 15:25
  • 1
    @JoeyHerrington: Do you have targetFramework set to 4.5 or higher in your web.config? Commented Aug 18, 2016 at 17:32
  • 1
    Also, why are you using HttpClient in the first place? If you need synchronous web requests, use WebClient. HttpClient was specifically designed for async/await-style code, and Result and friends can deadlock. Commented Aug 18, 2016 at 17:33
  • 2
    @JoeyHerrington: Long term, I'd definitely say go with 4.5 and upgrade the stuff it breaks. Short term you can try setting aspnet:UseTaskFriendlySynchronizationContext instead (details in the original blog post). That might work. Commented Aug 19, 2016 at 12:56

1 Answer 1

4

I just had this issue and would like to suggest this as the answer.

In your web.config make sure you have the following:

  <system.Web>
      <httpRuntime targetFramework="4.5" />
  </system.Web>

and

<appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />     
</appSettings>

See more info here:

https://blogs.msdn.microsoft.com/webdev/2012/11/19/all-about-httpruntime-targetframework/

It seems to just crash randomly if you don't do this. I am a big fan of backwards compatibility but not when it just crashes if you don't opt in to the new way.

(Suggested in comment from Stephen Cleary)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.