diff --git a/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs b/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs index e8f40e9b3fab..d942f580ffc7 100644 --- a/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs +++ b/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs @@ -144,13 +144,13 @@ private async Task HandleException(HttpContext context, ExceptionDispatchInfo ed context.Request.Path = _options.ExceptionHandlingPath; } var oldScope = _options.CreateScopeForErrors ? context.RequestServices : null; - using var scope = _options.CreateScopeForErrors ? context.RequestServices.GetRequiredService().CreateScope() : null; + await using AsyncServiceScope? scope = _options.CreateScopeForErrors ? context.RequestServices.GetRequiredService().CreateAsyncScope() : null; try { - if (scope != null) + if (scope.HasValue) { - context.RequestServices = scope.ServiceProvider; + context.RequestServices = scope.Value.ServiceProvider; } var exceptionHandlerFeature = new ExceptionHandlerFeature()