异步等待任务空引用异常 [英] async await Task null reference exception

查看:143
本文介绍了异步等待任务空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在维护的Webforms项目上收到NullReferenceExceptions.不足之处是没有针对此异常的堆栈跟踪,因为我的代码均未引起该异常.

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.

复制到剪贴板的异常详细信息提供零帮助:
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.

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.

当我查看非用户代码stacktrace时,看到以下内容(全部在mscorlib中):

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

通过加载页面或通过回发,该错误随机且不一致地发生.我添加System.Net.Http.HttpClient从其他站点公开的REST服务中提取数据后,问题就开始了.请注意,HttpClient仅包含用于发送/接收数据的异步方法.基于内部堆栈跟踪,我高度怀疑Task<>/async/await是罪魁祸首.

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.

为帮助进行故障排除,让我重申我正在.NET 4.6中编译的WebForms站点中运行此操作(如果您要告诉我的问题是我需要升级到MVC,请保存您的击键而且不要说).当HttpClient将所有内容公开为Task<>时,我通过调用以下方式同步调用它们:
Task<MyObject> myResultTask = restClient.GetResultAsync(); MyObject myResult = myResultTask.Result; // no await

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

提前谢谢!

推荐答案

我只是遇到了这个问题,并希望以此为答案.

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

在您的web.config中,请确保您具有以下内容:

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

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

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

在此处查看更多信息:

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.

(建议Stephen Cleary发表评论)

(Suggested in comment from Stephen Cleary)

这篇关于异步等待任务空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆