# Invalid Token error from Elastic APM .Net agent

**URL:** <https://discuss.elastic.co/t/invalid-token-error-from-elastic-apm-net-agent/380206>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [July 17, 2025, 8:42am UTC](https://discuss.elastic.co/t/invalid-token-error-from-elastic-apm-net-agent/380206 "2025-07-17T08:42:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinjalgranicus](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@kinjalgranicus](https://discuss.elastic.co/u/kinjalgranicus)\
**Post date:** [July 17, 2025, 8:42am UTC](https://discuss.elastic.co/t/invalid-token-error-from-elastic-apm-net-agent/380206/1 "2025-07-17T08:42:11Z")

</div>

Hello,

We have integrated .Net Elastic APM agent version 1.31 with IIS and our applications running on .Net framework 4.8 are throwing Invalid\_Token error.

Error 1:  
Message:Could not load type 'Invalid\_Token.0x010002C7' from assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Error 2:  
Could not load type 'Invalid\_Token.0x01000445' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Environment Variables in applicationhost.config for an app pool

```auto
<environmentVariables>
                    <add name="ELASTIC_OTEL_LOG_TARGETS" value="file" />
                    <add name="ELASTIC_APM_SERVER_URL" value="server_URL" />
                    <add name="COR_PROFILER" value="profiler_value" />
                    <add name="ELASTIC_APM_PROFILER_INTEGRATIONS" value="C:\Program Files\Elastic\APM Agent\integrations.yml" />
                    <add name="ELASTIC_APM_FLUSH_INTERVAL" value="30s" />
                    <add name="ELASTIC_APM_SERVICE_NAME" value="service_name" />
                    <add name="COR_ENABLE_PROFILING" value="1" />
                    <add name="ELASTIC_APM_ENABLED" value="True" />
                    <add name="ELASTIC_APM_SECRET_TOKEN" value="token" />
                    <add name="ELASTIC_OTEL_LOG_LEVEL" value="error" />
                    <add name="COR_PROFILER_PATH" value="C:\Program Files\Elastic\APM Agent\elastic_apm_profiler.dll" />
                    <add name="ELASTIC_APM_PROFILER_LOG" value="error" />
                    <add name="ELASTIC_APM_PROFILER_LOG_DIR" value="D:\ProgramData\elastic\apm-agent-dotnet\logs" />
                    <add name="ELASTIC_APM_ENVIRONMENT" value="dev" />
                    <add name="ELASTIC_APM_SPAN_COMPRESSION_ENABLED" value="True" />
                    <add name="ELASTIC_APM_METRICS_INTERVAL" value="60s" />
                    <add name="COMPlus_LoaderOptimization" value="1" />
                    <add name="ELASTIC_APM_TRANSACTION_MAX_SPANS" value="100" />
                    <add name="ELASTIC_APM_TRANSACTION_SAMPLE_RATE" value="0.5" />
                    <add name="ELASTIC_APM_VERIFY_SERVER_CERT" value="True" />
                    <add name="ELASTIC_APM_PROFILER_HOME" value="C:\Program Files\Elastic\APM Agent" />
                    <add name="ELASTIC_OTEL_LOG_DIRECTORY" value="D:\ProgramData\elastic\apm-agent-dotnet\logs" />
                </environmentVariables>

```

These errors are only seen when APM agent is enabled. When disabled, these errors are not logged.

It seems Elastic APM agent is corrupting metadata during the application initialization phase.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [July 18, 2025, 9:51am UTC](https://discuss.elastic.co/t/invalid-token-error-from-elastic-apm-net-agent/380206/2 "2025-07-18T09:51:55Z")

</div>

Hi @kinjalgranicu,

Welcome to the community! I see you also have raised a GitHub issue [here](https://github.com/elastic/apm-agent-dotnet/issues/2629) which would be good to follow for updates. I see that you're using later than .NET 4.7.2, but there is a note on supported compatibility for .NET 6+ in the supported [technologies documentation](https://www.elastic.co/docs/reference/apm/agents/dotnet/supported-technologies):

> While this library **should** work on .NET Core 2.0+, we limit our support to only those versions currently supported by Microsoft - .NET 6.0 and newer.

Do you have any other error information aside from what you've shared already? If not, I would recommend [enabling debug mode](https://www.elastic.co/docs/troubleshoot/observability/apm-agent-dotnet/apm-net-agent) and sharing the debug logs on the issue.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![kinjalgranicus](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@kinjalgranicus](https://discuss.elastic.co/u/kinjalgranicus)\
**Post date:** [July 22, 2025, 5:31am UTC](https://discuss.elastic.co/t/invalid-token-error-from-elastic-apm-net-agent/380206/3 "2025-07-22T05:31:40Z")

</div>

Hi @carly.richmond ,

Apologies for the late reply.

[APM\_Logs\_22\_Jul](https://west.weknowgovernment.com/home/showdocument?id=1622&t=638887410059500700)

As requested, logs are attached above.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [July 22, 2025, 10:28am UTC](https://discuss.elastic.co/t/invalid-token-error-from-elastic-apm-net-agent/380206/4 "2025-07-22T10:28:38Z")

</div>

Thanks for sharing. Can you confirm which auto-instrumentation type you are using? I'm guessing that you are using the [profiler auto-instrumentation](https://www.elastic.co/docs/reference/apm/agents/dotnet/setup-auto-instrumentation) option based on this entry in the logs:

> {Service} Detected agent activation method: profiler

But I also see a duplicate instantiation error so I want to check for sure:

> The singleton APM agent has already been instantiated and can no longer be configured. Reusing existing instance.

Looking at the logs in more detail, it looks like you may have an authorization issue:

> Error] - {SystemTotalCpuProvider} Error instantiating 'Processor' performance counter. Process does not have permissions to read performance counters. See [Metrics | APM .NET agent](https://www.elastic.co/guide/en/apm/agent/dotnet/current/metrics.html#metrics-system) to see how to configure. +-\> Exception: System.UnauthorizedAccessException: Access to the registry key 'Global' is denied.

Can you try and follow the steps in the link above (see [here](https://www.elastic.co/guide/en/apm/agent/dotnet/current/metrics.html#metrics-system)) and see if that resolves your issue?

---

<div class="post-metadata">

**Author:** ![kinjalgranicus](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@kinjalgranicus](https://discuss.elastic.co/u/kinjalgranicus)\
**Post date:** [July 23, 2025, 10:09am UTC](https://discuss.elastic.co/t/invalid-token-error-from-elastic-apm-net-agent/380206/5 "2025-07-23T10:09:13Z")

</div>

Hi @carly.richmond

Yes, we are using profiler auto instrumentation.

As per your suggestion, we have made the changes mentioned under [Metrics | APM .NET agent](https://www.elastic.co/docs/reference/apm/agents/dotnet/metrics#metrics-system) and we will monitor this fix for few days.

Thanks.

---

<div class="post-metadata">

**Author:** ![kinjalgranicus](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@kinjalgranicus](https://discuss.elastic.co/u/kinjalgranicus)\
**Post date:** [July 24, 2025, 5:15am UTC](https://discuss.elastic.co/t/invalid-token-error-from-elastic-apm-net-agent/380206/6 "2025-07-24T05:15:09Z")

</div>

Hi @carly.richmond ,

The error message is seen again.

Logs → [APM\_Logs\_23\_Jul](https://west.weknowgovernment.com/home/showdocument?id=1624&t=638889128156966696)

Looks like some more errors are recorded in the logs.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [July 24, 2025, 4:11pm UTC](https://discuss.elastic.co/t/invalid-token-error-from-elastic-apm-net-agent/380206/7 "2025-07-24T16:11:23Z")

</div>

Thanks for sharing @kinjalgranicus. It looks like the thread is being killed:

> 2025-07-23 21:04:51.579 +00:00][126][Error] - {BackendCommComponentBase (CentralConfigurationFetcher)} WorkLoop Current thread: `ElasticApmCentralConfigurationFetcher' (managed ID: 126) +-\> Exception: System.Threading.ThreadAbortException: Thread was being aborted.  
> at System.Threading.Monitor.ObjWait(Boolean exitContext, Int32 millisecondsTimeout, Object obj)  
> at System.Threading.ManualResetEventSlim.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)  
> at System.Threading.Tasks.Task.SpinThenBlockingWait(Int32 millisecondsTimeout, CancellationToken cancellationToken)  
> at System.Threading.Tasks.Task.InternalWait(Int32 millisecondsTimeout, CancellationToken cancellationToken)  
> at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)  
> at Elastic.Apm.BackendComm.CentralConfig.CentralConfigurationFetcher.WorkLoopIteration()  
> at Elastic.Apm.BackendComm.BackendCommComponentBase.WorkLoop()

This may be a long shot as I'm not a .NET developer, but I did come across [this solution](https://discuss.elastic.co/t/auto-instrumention-metrics-work-traces-not-received/295559/21) where they had thread issues relating to shared DLLs. Can you see if amending your configuration works? If not I think it will need to fed back onto the GitHub issue.
