# PayloadSenderV2 NullReferenceException when stopping a dotnet core IHostedService background service

**URL:** <https://discuss.elastic.co/t/payloadsenderv2-nullreferenceexception-when-stopping-a-dotnet-core-ihostedservice-background-service/287122>\
**Category:** APM\
**Tags:** dotnet, language-clients\
**Created:** [October 19, 2021, 4:28pm UTC](https://discuss.elastic.co/t/payloadsenderv2-nullreferenceexception-when-stopping-a-dotnet-core-ihostedservice-background-service/287122 "2021-10-19T16:28:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![diosifidis](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@diosifidis](https://discuss.elastic.co/u/diosifidis)\
**Post date:** [October 19, 2021, 4:28pm UTC](https://discuss.elastic.co/t/payloadsenderv2-nullreferenceexception-when-stopping-a-dotnet-core-ihostedservice-background-service/287122/1 "2021-10-19T16:28:31Z")

</div>

**Kibana version** :  
7.13.4

**Elasticsearch version** :  
7.13.4

**APM Agent language and version** :  
C#, 1.11.1

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**:  
Nuget package

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:

Hi,

I have a .net core(3.1) application which essentially is a background service running as a windows service. I'm implementing the "BackgroundService" from .net similar to the example in the following link: [Background tasks with hosted services in ASP.NET Core | Microsoft Docs](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-5.0&tabs=visual-studio)

I'm configuring the apm agent by using ".UseAllElasticApm()" on the hosted service and "UseElasticApm" on the Redis connection that I have.

My issue is that I'm getting an exception when my service tries stop by calling  
`await base.StopAsync(cancellationToken);`

The error is: "WARN Elastic.Apm {PayloadSenderV2} Exception during execution of the filter on transaction  
System.NullReferenceException: Object reference not set to an instance of an object.  
at Elastic.Apm.Filters.ErrorContextSanitizerFilter.Filter(IError error)  
at Elastic.Apm.Report.PayloadSenderV2.TryExecuteFilter[T](List`1 filters, T item)"

**Steps to reproduce** :

1. Run a dotnet background service
2. Call `base.StopAsync()`
3. 

**Errors in browser console (if relevant)**:

**Provide logs and/or server output (if relevant)**:

 ![ErrorLog](https://us1.discourse-cdn.com/elastic/original/3X/4/9/49b4b8e00cb7167a1f1202e7ec3f633f623b298f.png)  
 ![.net core background service](https://us1.discourse-cdn.com/elastic/original/3X/c/6/c6d4ca8b29f263791bbddddf8f15804d81a66e8b.png)

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [October 25, 2021, 4:06am UTC](https://discuss.elastic.co/t/payloadsenderv2-nullreferenceexception-when-stopping-a-dotnet-core-ihostedservice-background-service/287122/2 "2021-10-25T04:06:13Z")

</div>

Hi @diosifidis. The following error

> [@diosifidis](#):
>
> The error is: "WARN Elastic.Apm {PayloadSenderV2} Exception during execution of the filter on transaction  
> System.NullReferenceException: Object reference not set to an instance of an object.  
> at Elastic.Apm.Filters.ErrorContextSanitizerFilter.Filter(IError error)  
> at Elastic.Apm.Report.PayloadSenderV2.TryExecuteFilter[T](List`1 filters, T item)"

I believe is the same issue as

> <https://github.com/elastic/apm-agent-dotnet/issues/1503>
>
> \### APM Agent version
> 1.11.1
> 
> \### Environment
> APM server 7.14
> 
> \*\*.NET Fram…ework/Core name and version\*\* 
> NET Core 3.1.413
> 
> \*\*Application Target Framework(s)\*\*
> netcoreapp3.1
> 
> \### Describe the bug
> PayloadSenderV2 throws exception when Asp.net core HealthChecks is checking a dependency which fails.
> 
> \### To Reproduce
> I noticed a Warning in log when Health Check in my application is testing a failed dependency which is down and health check is reporting an error. I've tried to reproduce this issue in smaller sample but so far with no luck.
> 
> \#### Expected behavior
> No Exception
> 
> \#### Actual behavior
> 
> \`\`\`
> \[11:41:45 WRN\] {PayloadSenderV2} Exception during execution of the filter on transaction
> System.NullReferenceException: Object reference not set to an instance of an object.
> at Elastic.Apm.Filters.ErrorContextSanitizerFilter.Filter(IError error)
> at Elastic.Apm.Report.PayloadSenderV2.TryExecuteFilter\[T\](List\`1 filters, T item)
> \`\`\`
> 
> Error value which is received doesn't have Context filled. I think this may be the case for health check requests that they are not normal Http Request but they are executed from healthcheck-ui automatically every x seconds, but this is only my guess.
> \`\`\`
> {Error{Id: 71062bdb2bbf2fcf2f5ce5307c818e7c, TraceId: fce5e93fe1dcc643b1b69e1172abd2ae, ParentId: 9a8d3663dd710241, TransactionId: 9a8d3663dd710241}}
> ConfigSnapshot: {Elastic.Apm.Config.ConfigSnapshotFromReader}
> Context: null
> Culprit: "Failed outgoing HTTP request"
> Exception: {CapturedException{Type: System.Net.Http.HttpRequestException, Message: No connection could be made because the target machine actively refused it., Handled: False, Code: null}}
> Id: "71062bdb2bbf2fcf2f5ce5307c818e7c"
> Log: null
> ParentId: "9a8d3663dd710241"
> Timestamp: 1633085851482142
> TraceId: "fce5e93fe1dcc643b1b69e1172abd2ae"
> Transaction: {TransactionData{IsSampled: False, Type: request}}
> TransactionId: "9a8d3663dd710241"
> \`\`\`
> 
> \`\`\`
> https://github.com/elastic/apm-agent-dotnet/blob/1.11/src/Elastic.Apm/Filters/ErrorContextSanitizerFilter.cs
> internal class ErrorContextSanitizerFilter
> {
> public IError Filter(IError error)
> {
> if (error is Error realError)
> {
> // I suspect this realError.Context throws exception as it was null
> if (realError.Context.Request?.Headers != null && realError.ConfigSnapshot != null)
> {
> foreach (var key in realError.Context?.Request?.Headers?.Keys.ToList())
> {
> if (WildcardMatcher.IsAnyMatch(realError.ConfigSnapshot.SanitizeFieldNames, key))
> realError.Context.Request.Headers\[key\] = Consts.Redacted;
> }
> }
> }
> 
> return error;
> }
> }
> \`\`\`
> 
> Could you give some guidance how I could track this error?

This has been addressed in

> <https://github.com/elastic/apm-agent-dotnet/pull/1510>
>
> Fixes elastic/apm-agent-dotnet#1503

and will be in the next release.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 15, 2021, 12:07am UTC](https://discuss.elastic.co/t/payloadsenderv2-nullreferenceexception-when-stopping-a-dotnet-core-ihostedservice-background-service/287122/3 "2021-11-15T00:07:04Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
