diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs index b27565a0f42..3db7ae6fb25 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs @@ -960,6 +960,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan int numberOfUnSubscribedEvents = 0; int numberOfSubscribedEvents = 0; int numberOfExceptionCallbacks = 0; + bool exceptionHandled = false; // configure SDK this.tracerProvider = Sdk.CreateTracerProviderBuilder() @@ -1004,18 +1005,18 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan }) .Build(); + TestMiddleware.Create(builder => builder + .UseExceptionHandler(handler => + handler.Run(async (ctx) => + { + exceptionHandled = true; + await ctx.Response.WriteAsync("handled"); + }))); + using (var client = this.factory .WithWebHostBuilder(builder => { builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders()); - builder.Configure(app => app - .UseExceptionHandler(handler => - { - handler.Run(async (ctx) => - { - await ctx.Response.WriteAsync("handled"); - }); - })); }) .CreateClient()) { @@ -1033,6 +1034,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan Assert.Equal(0, numberOfExceptionCallbacks); Assert.Equal(0, numberOfUnSubscribedEvents); Assert.Equal(2, numberOfSubscribedEvents); + Assert.True(exceptionHandled); } public void Dispose() diff --git a/test/TestApp.AspNetCore/Program.cs b/test/TestApp.AspNetCore/Program.cs index b68fcf238fa..d3fae73ff77 100644 --- a/test/TestApp.AspNetCore/Program.cs +++ b/test/TestApp.AspNetCore/Program.cs @@ -60,6 +60,8 @@ public static void Main(string[] args) app.UseMiddleware(); + app.AddTestMiddleware(); + app.Run(); } } diff --git a/test/TestApp.AspNetCore/TestMiddleware.cs b/test/TestApp.AspNetCore/TestMiddleware.cs new file mode 100644 index 00000000000..67c9b9fa6ca --- /dev/null +++ b/test/TestApp.AspNetCore/TestMiddleware.cs @@ -0,0 +1,37 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +namespace TestApp.AspNetCore; + +public static class TestMiddleware +{ + private static readonly AsyncLocal?> Current = new(); + + public static IApplicationBuilder AddTestMiddleware(this IApplicationBuilder builder) + { + if (Current.Value is { } configure) + { + configure(builder); + } + + return builder; + } + + public static void Create(Action action) + { + Current.Value = action; + } +}