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):
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.
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:
Example Controller:
public Task<IActionResult> GetDataFromMyMongoDB(string id){
var data = await _service.GetData(id);
if(data == null)
return BadRequest("Error");
return Ok(data);
}
Service:
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;
}
}
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.
EDIT: 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.
Stack Trace:
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!
There is probably nothing wrong. Sounds like AppInsights is being a bit paranoid.
This is most commonly caused by one of two things.
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".
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With