Invalid Token error from Elastic APM .Net agent

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

<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.

Hi @kinjalgranicu,

Welcome to the community! I see you also have raised a GitHub issue here 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:

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 and sharing the debug logs on the issue.

Hope that helps!

Hi @carly.richmond ,

Apologies for the late reply.

APM_Logs_22_Jul

As requested, logs are attached above.

Thanks for sharing. Can you confirm which auto-instrumentation type you are using? I'm guessing that you are using the profiler 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 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) and see if that resolves your issue?

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 and we will monitor this fix for few days.

Thanks.

1 Like

Hi @carly.richmond ,

The error message is seen again.

Logs → APM_Logs_23_Jul

Looks like some more errors are recorded in the logs.

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 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.