aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Add analyzer for custom IProblemDetailsWriter registration

Open david-acker opened this issue 1 year ago • 8 comments

Add analyzer for custom IProblemDetailsWriter registration

Adds an analyzer that checks that custom IProblemDetailsWriters are registered in the correct order, relative to MvcServiceCollectionExtensions method calls.

Description

If a custom IProblemDetailsWriter is registered after a MvcServiceCollectionExtensions method, the DefaultProblemDetailsWriter will be used instead of the custom IProblemDetailsWriter.

This analyzer reports a warning level diagnostic when an IProblemDetailsWriter is registered (using AddTransient, AddScoped, or AddSingleton) and appears after a call to a MvcServiceCollectionExtensions method (AddControllers, AddControllersWithViews, AddMvc, or AddRazorPages).

Diagnostic

Id: ASP0026 Title: Custom IProblemDetailsWriter is incorrectly configured Message: The custom IProblemDetailsWriter must be registered before calling AddControllers, AddControllersWithViews, AddMvc, or AddRazorPages. Severity: Warning

Code Examples:

Diagnostic Reported:

services.AddControllers();

// Diagnostic reported on method invocation below
services.AddTransient<IProblemDetailsWriter, SampleProblemDetailsWriter>();

No Diagnostic Reported:

services.AddTransient<IProblemDetailsWriter, SampleProblemDetailsWriter>();

services.AddControllers();

Analyzer Infrastructure Changes

  • Brings over StartupAnalyzer-related infrastructure from the legacy Microsoft.AspNetCore.Analyzers project

Fixes #48180

david-acker avatar Jul 10 '23 01:07 david-acker

Thanks for your PR, @david-acker. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

ghost avatar Jul 10 '23 01:07 ghost

@david-acker, Are you still working on this one? No pressure, just asking because it's been sitting in draft state for a while.

adityamandaleeka avatar Jan 31 '24 01:01 adityamandaleeka

I'm planning on working on this again fairly soon now that the API has finally approved.

david-acker avatar Jan 31 '24 03:01 david-acker

@captainsafia PTAL

adityamandaleeka avatar Mar 04 '24 17:03 adityamandaleeka

/azp run

captainsafia avatar Mar 04 '24 18:03 captainsafia

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar Mar 04 '24 18:03 azure-pipelines[bot]

@david-acker I saw you posted a question in the original issue about moving StartupAnalyzer-related infrastructure to the analyzers that get shipped in the refpack. I'm in favor of incorproating a StartupAnalyzer in that project and using source-sharing to bring in helpful dependencies like StartupFacts.

captainsafia avatar Mar 04 '24 19:03 captainsafia

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.