# APM NET agent errors: An unhandled exception was thrown by the application

**URL:** <https://discuss.elastic.co/t/apm-net-agent-errors-an-unhandled-exception-was-thrown-by-the-application/337416>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [July 3, 2023, 6:18am UTC](https://discuss.elastic.co/t/apm-net-agent-errors-an-unhandled-exception-was-thrown-by-the-application/337416 "2023-07-03T06:18:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![agonzalez](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@agonzalez](https://discuss.elastic.co/u/agonzalez)\
**Post date:** [July 3, 2023, 6:18am UTC](https://discuss.elastic.co/t/apm-net-agent-errors-an-unhandled-exception-was-thrown-by-the-application/337416/1 "2023-07-03T06:18:14Z")

</div>

I am testing APM and since i have included NET APM agent on my .NET 7 service in linux I am seeing this error repeated so many times. Can anyone tell me how to fix this? or what is the problem?

```auto
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HMRMFR3CE0UB", Request id "0HMRMFR3CE0UB:00000001": An unhandled exception was thrown by the application.
      System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).
         at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)
         at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.<>c __DisplayClass0_0.<<HandleAsync>g__ Handle|0>d.MoveNext()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
         at Elastic.Apm.AspNetCore.ApmMiddleware.InvokeAsync(HttpContext context)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

```

---

<div class="post-metadata">

**Author:** ![agonzalez](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@agonzalez](https://discuss.elastic.co/u/agonzalez)\
**Post date:** [July 6, 2023, 2:16pm UTC](https://discuss.elastic.co/t/apm-net-agent-errors-an-unhandled-exception-was-thrown-by-the-application/337416/2 "2023-07-06T14:16:09Z")

</div>

anyone?

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [July 14, 2023, 2:19pm UTC](https://discuss.elastic.co/t/apm-net-agent-errors-an-unhandled-exception-was-thrown-by-the-application/337416/3 "2023-07-14T14:19:49Z")

</div>

Hi @agonzalez,

Here is what I can say based on the call-stack you pasted:

The only stack frame I see from the agent is `Elastic.Apm.AspNetCore.ApmMiddleware.InvokeAsync` - that is the one which captures the incoming HTTP call. Now.. I don't really know why it calls `Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware`, but here is an idea: we actually have another way to turn on the agent, which does not rely on `Elastic.Apm.AspNetCore.ApmMiddleware` - it's be fairly easy to test it: You could call `UseElasticApm` on the `HostBuilder`, [like this](https://github.com/elastic/apm-agent-dotnet/blob/main/test/integrations/applications/SampleConsoleNetCoreApp/Program.cs#L27). So let's try to enable the agent in `Program.cs` instead of in `Startup.cs` as you probably do it now. That way of adding the agent uses a different mechanism to capture the incoming call and does not rely on the APM middleware.

Alternative you could also try the profiler: [Profiler Auto instrumentation | APM .NET Agent Reference [1.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/dotnet/current/setup-auto-instrumentation.html#setup-auto-instrumentation) - same story with that: it does not use the middleware either.

Could you please give this a try? Otherwise we could look into why this is happening, but for that I'd need more info - exact versions, turn on line numbers for the call stacks, exact agent version, and possible a small reproducer.

---

<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:** [August 11, 2023, 2:20pm UTC](https://discuss.elastic.co/t/apm-net-agent-errors-an-unhandled-exception-was-thrown-by-the-application/337416/4 "2023-08-11T14:20:21Z")

</div>

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