AddEndpointFilter
throws exception shown below in .NET 7 (7.0.102) at program start under debug. Used IDE Microsoft Visual Studio Professional 2022 (64-bit) – Current Version 17.6.3
System.InvalidOperationException
HResult=0x80131509
Message=A suitable constructor for type 'WebApplication3.OperationCancelledFilter' could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided.
Source=Microsoft.Extensions.DependencyInjection.Abstractions
StackTrace:
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable`1[]& matchingParameterMap) in /_/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs:line 216
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type instanceType, Type[] argumentTypes) in /_/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs:line 97
at Microsoft.AspNetCore.Http.EndpointFilterExtensions.AddEndpointFilter[TBuilder,TFilterType](TBuilder builder)
This is full code example.
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app
.MapGet("/", () => "Hello World!")
.AddEndpointFilter<OperationCancelledFilter>();
app.Run();
class OperationCancelledFilter : IEndpointFilter
{
private readonly ILogger<OperationCancelledFilter> _logger;
public OperationCancelledFilter(
ILogger<OperationCancelledFilter> logger
)
{
_logger = logger;
}
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
try
{
return await next(context);
}
catch (OperationCanceledException ex)
{
_logger.LogDebug(ex, "Request was cancelled");
return Results.StatusCode(499);
}
}
}
If I click on continue application starts OK and breakpoint is hit so it works. Only problem is during startup with unhandled exception.
It works in this way without any unandled exception.
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app
.MapGet("/", () => "Hello World!")
.AddEndpointFilter(async (context, next) =>
{
var logger = context.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger("program");
try
{
return await next(context);
}
catch (OperationCanceledException ex)
{
logger.LogDebug(ex, "Request was cancelled");
return Results.StatusCode(499);
}
});
app.Run();
2
Answers
Now I deleted constructor at all and unhandled exception is still there.
Project is default ASP.NET Core created from empty template.
Try fully qualify the interface type:
The only option I see – you have some other library installed which defines the same interface. Otherwise was not able to repro – current code works fine.
P.S.
Are you sure there are no other parameters in
OperationCancelledFilter
ctor? Because formatting seems a bit suspicious.