Skip to content

[Refactoring] Add back middlesware that are registered by WebApplication #45222

Open
@captainsafia

Description

@captainsafia

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
  • Usage

Severity Level

  • Error
  • Warning
  • Info
  • Hidden

Usage Scenarios

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

Result:

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.

Metadata

Metadata

Assignees

Labels

analyzerIndicates an issue which is related to analyzer experienceapi-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions