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

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?

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)


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

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