Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint filter factory was called twice. #59562

Open
1 task done
CoreDX9 opened this issue Dec 19, 2024 · 2 comments
Open
1 task done

Endpoint filter factory was called twice. #59562

CoreDX9 opened this issue Dec 19, 2024 · 2 comments
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

Comments

@CoreDX9
Copy link

CoreDX9 commented Dec 19, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Minimal-API's endpoint filter factory was called twice.

Expected Behavior

Minimal-API's endpoint filter factory was called once.

Steps To Reproduce

app.MapGet("/api/a", () => { })
    .AddEndpointFilterFactory((ctx, next) =>
    {
        ctx.ApplicationServices.GetService<ILogger<WebApplication>>()!.LogError("Enter!!!!!!!!!!!!");
        return (ctxf) => next(ctxf);
    });

Image

Exceptions (if any)

No response

.NET Version

No response

Anything else?

  • ASP.NET Core version: 8.0 and 9.0.
  • VS version: 17.12.1
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Dec 19, 2024
@martincostello martincostello added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Dec 19, 2024
@oussamatecnique
Copy link

I couldn't reproduce it, do you have more details?
also is this affecting the registering of endpoint filters?

@CoreDX9
Copy link
Author

CoreDX9 commented Jan 1, 2025

The endpoint metadata obtained from the two calls were different objects, so I believe the result of the first call was discarded. This should not affect the registration of endpoint filters.

I think the first call is meaningless. If many endpoints are registered, unnecessary garbage collection and increased startup time may occur.

This is a sample. Both MainWebApplication and MainHostBuilder will output logs twice on .NET 8.0 and 9.0. Browser auto run is closed.

namespace WebApplication1;

public class Program
{
    public const string testStr = "Has a string in endpoint metadata.";
    public static void Main(string[] args)
    {
        MainWebApplication(args);
        //MainHostBuilder(args);
    }

    public static void MainWebApplication(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        var app = builder.Build();

        var endpoint = app.MapGet("/", () => { });

        endpoint.Add(static endpotintBuilder =>
        {
            endpotintBuilder.FilterFactories.Add((ctx, next) =>
            {
                ctx.ApplicationServices.GetService<ILogger<WebApplication>>()!.LogError("Enter!!!!!!!!!!!!");

                if (!endpotintBuilder.Metadata.Any(x => x is testStr))
                {
                    endpotintBuilder.Metadata.Add(testStr);
                    endpotintBuilder.ApplicationServices.GetService<ILogger<WebApplication>>()!.LogError(testStr);

                    return ctxf =>
                    {
                        ctxf.HttpContext.RequestServices.GetService<ILogger<WebApplication>>()!.LogError("A filter with logging added.");
                        return next(ctxf);
                    };
                }

                return (ctxf) => next(ctxf);
            });
        });

        app.Run();
    }

    public static void MainHostBuilder(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services) { }
    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            var endpoint = endpoints.MapGet("/", () => { });

            endpoint.Add(static endpotintBuilder =>
            {
                endpotintBuilder.FilterFactories.Add((ctx, next) =>
                {
                    ctx.ApplicationServices.GetService<ILogger<WebApplication>>()!.LogError("Enter!!!!!!!!!!!!");

                    if (!endpotintBuilder.Metadata.Any(x => x is Program.testStr))
                    {
                        endpotintBuilder.Metadata.Add(Program.testStr);
                        endpotintBuilder.ApplicationServices.GetService<ILogger<WebApplication>>()!.LogError(Program.testStr);

                        return ctxf =>
                        {
                            ctxf.HttpContext.RequestServices.GetService<ILogger<WebApplication>>()!.LogError("A filter with logging added.");
                            return next(ctxf);
                        };
                    }

                    return (ctxf) => next(ctxf);
                });
            });
        });
    }
}

Output :

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5219
fail: Microsoft.AspNetCore.Builder.WebApplication[0]
      Enter!!!!!!!!!!!!
fail: Microsoft.AspNetCore.Builder.WebApplication[0]
      Has a string in endpoint metadata.
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: F:\source\repos\WebApplication1\WebApplication1
// Before open browser
// =====================================================================
// After open browser
fail: Microsoft.AspNetCore.Builder.WebApplication[0]
      Enter!!!!!!!!!!!!
fail: Microsoft.AspNetCore.Builder.WebApplication[0]
      Has a string in endpoint metadata.
fail: Microsoft.AspNetCore.Builder.WebApplication[0]
      A filter with logging added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Projects
None yet
Development

No branches or pull requests

3 participants