aspnet-api-versioning icon indicating copy to clipboard operation
aspnet-api-versioning copied to clipboard

Report in DefaultApiVersionReporter disposed RequestServices

Open jacopocinaark opened this issue 1 year ago • 1 comments
trafficstars

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

In my application when the HttpRequest is done the RequestServices in the HttpContext is already disposed. the Report function is called after the HttpContext is disposed so an Exception is throw (searching for ISunsetPolicyManager). image (14) 97f253ad-250a-486e-b8fd-076b48282baf The problem was not happening before the refactoring in Asp.Versioning package

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

6.0

Anything else?

No response

jacopocinaark avatar Aug 29 '24 13:08 jacopocinaark

Something is amiss here. This is strange. There is no path where this can happen by default. I presume you are using version 6.4.x.

Minimal APIs

Versions and policies are reported as soon as the response starts because that is the time to write headers:

https://github.com/dotnet/aspnet-api-versioning/blob/db4031872105fd882f8391b35acd156c94cb65ed/src/AspNetCore/WebApi/src/Asp.Versioning.Http/net6.0/Routing/ReportApiVersionsDecorator.cs#L26

Controllers

Virtually the same approach is used for controllers, but it is triggered by an IActionFilter applied to an app, controller, or action:

https://github.com/dotnet/aspnet-api-versioning/blob/db4031872105fd882f8391b35acd156c94cb65ed/src/AspNetCore/WebApi/src/Asp.Versioning.Mvc/ReportApiVersionsAttribute.cs#L49

Something strange is happening. The current HttpContext and its IServiceProvider, even scoped, should still be available at this point in time. Are you able to put together the world's simplest repro? That would help troubleshoot where things have gone wrong.

FWIW, this issue shouldn't exist in .NET 8 since ISunsetPolicyManager was added as an injected service to the constructor. This wasn't possible in .NET 6 without introducing a breaking change (which I will only do at major versions).

https://github.com/dotnet/aspnet-api-versioning/blob/3fc071913dcded23eeb5ebe55bca44f3828488bf/src/Common/src/Common/DefaultApiVersionReporter.cs#L40

commonsensesoftware avatar Sep 10 '24 16:09 commonsensesoftware

Following up. Is this still an issue? Did you ever resolve it?

commonsensesoftware avatar Nov 15 '25 22:11 commonsensesoftware