dotnet / dotnet/aspnetcore

[Refactoring] Add back middlesware that are registered by WebApplication

Open
#45,222 8 comments 0 reactions 1 assignee Claimed by @BrennanConroy View on GitHub
analyzer api-needs-work area-minimal
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

## Background and Motivation

## Proposed Analyzer

For middlesware that are registered automatically by the WebApplication, consider a refactoring to manually add them back to the application.

### Analyzer Behavior and Message

### Category

- [ ] Design
- [ ] Documentation
- [ ] Globalization
- [ ] Interoperability
- [ ] Maintainability
- [ ] Naming
- [ ] Performance
- [ ] Reliability
- [ ] Security
- [ ] Style
- [x] Usage

### Severity Level

- [ ] Error
- [ ] Warning
- [x] Info
- [ ] Hidden

## Usage Scenarios

```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build(); // <-- underline 'app' with refactor suggestion
app.Run();
```

```csharp
var app = WebApplication.Create(args); // <-- underline 'app' with refactor suggestion
app.Run();
```

Result:
```csharp
var app = WebApplication.Create(args);
if (app.HostingEnvironment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();

app.UseAuthentication(); // detect any `UseAuthentication` call and omit this if there is one
app.UseAuthorization(); // detect any `UseAuthorization` call and omit this if there is one
// additionally, place after any existing `UseAuthentication` call

// detect user middleware/endpoints and put them here

app.UseEndpoints(e => {});
app.Run();
```

## Risks

We will not be able to place the auto-injected middleware correctly in all situations, that should be fine since this refactor is to help users be explicit about their middleware order.

Another risk is if we add more implicit middleware we'll want to keep the refactoring up to date.

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.