Skip to content

AddEndpointFilter Exception #61515

@mygoodbay

Description

@mygoodbay

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMemoryCache();
//builder.Services.AddScoped<AdminEndpointFilter>();

var app = builder.Build();

var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };

app.MapGet("/weatherforecast", () =>
{
    var forecast = Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
}).AddEndpointFilter<AdminEndpointFilter>();

app.Run();

internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

class AdminEndpointFilter(IMemoryCache memoryCache) : IEndpointFilter
{
    private readonly IMemoryCache memoryCache = memoryCache;

    public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
    {
        return await next(context);
    }
}

//This is a net8 minimalApi demo.
// Add AddEndpointFilter<AdminEndpointFilter>()
//Exception raised: “System.InvalidOperationException” (in Microsoft.Extensions.DependencyInjection.Abstractions.dll)
//Remove it and you're good to go.

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.Needs: ReproIndicates that the team needs a repro project to continue the investigation on this issueStatus: No Recent Activityarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions