dotnet / dotnet/aspnetcore

ASP0001 warning when used inside app.MapWhen

Open
#41,030 4 comments 1 reaction 1 assignee Claimed by @Youssef1313 View on GitHub
analyzer area-minimal area-mvc bug
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

I try to create a webapp with a port dedicated to grpc and another port dedicated to openapi with dotnet6

```
var app = builder.Build();

app.MapWhen(context => context.Connection.LocalPort == myGrpcPort, grpcApp =>
{
grpcApp.UseRouting();
grpcApp.UseAuthentication();
grpcApp.UseAuthorization();
grpcApp.UseEndpoints(routeBuilder =>
{
routeBuilder.MapGrpcService();
routeBuilder.MapCodeFirstGrpcReflectionService();
});
});
app.MapWhen(context => context.Connection.LocalPort == myOpenApiPort, openapiApp =>
{
openapiApp.UseRouting();
openapiApp.UseAuthentication();
openapiApp.UseAuthorization(); //this triggers the warning
openapiApp.UseEndpoints(routeBuilder => routeBuilder.MapControllers());
```

When executing `dotnet run`

```
warning ASP0001: The call to UseAuthorization should appear between app.UseRouting() and app.UseEndpoints(..) for authorization to be correctly evaluated
```

If I comment the grpcApp part, the warning disappear

If I swap openapiApp before grpcApp, the warning is triggered in the grpc part

=> how can I avoid the warning in the second app.MapWhen ?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.