没有观察到任务的例外 [英] A Task's exception(s) were not observed

查看:448
本文介绍了没有观察到任务的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看到我的Application Insight Web应用程序的日志中的错误,但是我没有找到它在哪里,我的应用程序或Web api中没有发现任何奇怪的行为(.NET 4.5与Web API 2.0 ,使用OutputCache和 Web Api 2 Cache ,Azure中的MongoDB像Web App一样有2个实例):

I'm watching an error in my logs of Application Insight web application, but I don't find where it's coming and I don't find any strange behaviour in my app or web api (.NET 4.5 with Web API 2.0, using OutputCache and Web Api 2 Cache, MongoDB in Azure like Web App with 2 instances):


通过等待任务
或访问其异常属性,没有观察到任务的异常。因此,未知的
异常被finalizer thread.Object引用而不是
设置为一个对象的实例。

A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.Object reference not set to an instance of an object.

我找不到错误来自哪里,我会给你一些示例代码,我使用了很多我的Web Api和Javascript来获取数据:

I can't find where the error comes from, I will show you some example code, I'm using a lot of my Web Api and Javascript to get the data:

示例控制器:

public Task<IActionResult> GetDataFromMyMongoDB(string id){

         var data = await _service.GetData(id);

         if(data == null)
               return BadRequest("Error");

         return Ok(data);
}

服务:

public Task<ICollection<MyDto>> GetData(id)
{
    //Check security operations for example, or business login like this:

    var data = await _repositoryData.Where(d=>d.Id == id);

    //For example, sometimes I do some business logic operations in which
    // I use try ... catch (this code is just for show example code)

    try
    {
       var dataToJson = JSON.parse(data);

       //Dummy code dataJson to obtain a value of this and changed and update
       //to repository

       //** Code omited **//

       var bool = await _repository.UpdateManyAsync(data);

       if(bool == false)
         return null;

       else 
         {
            //Here I do typical mapper operations from my
            //entities to my DTOs 

            //** Code omited **//

            return mappedListDto; // <-- Type: ICollection<MyDto>
         }  

    }
    catch(Exception ex)
    {
       //Log in Elmah
       //Elmah stuff code here

       //** Code omited **//

       return null;

    }





}

我的存储库是进行简单操作的标准代码,在单元测试中工作正常,我已经使用我的网络应用程序多次,同时有超过100个用户没有问题,但在我的日志中,我没有任务。

My Repository is standard code for make simple operations and is working fine in unit testing, and I have used my web app many times and more than 100 users at the same time without problem, but in my Logs, I have the exception of the task.

我已经阅读了这个问题这一个也是,但我不明白如何在我的代码,我觉得很笨。

I have read this question and this one too, but I can't understand how to apply in my code, I feel stupid.

编辑:更多信息:在过去12小时内,我已经收到了这种类型的大约500个例外,但应用程序工作正常,在过去12个小时内我有大约167 K的要求。

More information: I have gotten about 500 exception of this type in the last 12 hours but the application is working fine, in the last 12 hours I have about 167 K request.

堆栈跟踪

System.AggregateException:
System.NullReferenceException:
   at System.Web.ThreadContext.AssociateWithCurrentThread (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Web.HttpApplication.OnThreadEnterPrivate (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Web.HttpApplication.System.Web.Util.ISyncContext.Enter (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Web.Util.SynchronizationHelper.SafeWrapCallback (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Threading.Tasks.Task.Execute (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)

任何想法?我真的很疯狂,谢谢你这么多!

Any ideas? I'm totally crazy with that... Thank you so much!

推荐答案

可能没有错。听起来像AppInsights是有点偏执。

There is probably nothing wrong. Sounds like AppInsights is being a bit paranoid.

这通常是由两件事之一造成的。

This is most commonly caused by one of two things.

一个是你的代码中的等待任何逻辑分支。这通常是使用 Task.WhenAny 的一些代码。我建议搜索你的代码WhenAny。

One is a "wait for any" kind of logical fork in your code. This is usually some code that uses Task.WhenAny. I recommend searching your code for "WhenAny".

另一个是火和忘记的逻辑。这通常是调用 Task.Run 的代码,然后忽略返回的任务。编译器非常擅长警告你有关忘记密码(因为几乎总是错误的),所以首先要检查的是你的编译器警告。

The other is a "fire and forget" kind of logic. This is usually code that calls Task.Run and then ignores the returned task. The compiler is pretty good at warning you about fire-and-forget code (since it's almost always a mistake), so the first thing to check is your compiler warnings.

因为你有一个单元测试套件,你可以连接一个处理程序 TaskScheduler.UnobservedTaskException ,这将捕获这些异常,然后运行/调试你的测试套件,看看哪个测试被击中。如果要安静下来AppInsights,您可以在生产代码中添加调用 UnobservedTaskExceptionEventArgs.SetObserved 的处理程序。

Since you have a unit test suite, you can hook up a handler for TaskScheduler.UnobservedTaskException, which will catch these exceptions, and then run/debug your test suite and see under which tests that gets hit. If you want to quiet down AppInsights, you can add a handler in your production code that calls UnobservedTaskExceptionEventArgs.SetObserved.

这篇关于没有观察到任务的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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