-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Comments
I couldn't reproduce it, do you have more details? |
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 :
|
Is there an existing issue for this?
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
Exceptions (if any)
No response
.NET Version
No response
Anything else?
The text was updated successfully, but these errors were encountered: