question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ExceptionHandlerMiddleware does not clear IApiVersioningFeature

See original GitHub issue

https://github.com/dotnet/aspnetcore/blob/d09b931628258850ba0c56d17b3731b72ddf88f5/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddleware.cs#L170-L171

If someone is trying to use both the ApiVersioning feature and the ExceptionHandler configuration that uses a path name, you will get an ambiguous routing exception on every error because the router uses the selected routes in the IApiVersioningFeature from the original request which contains the original route.

Here is the routing code from ApiVersioning: https://github.com/microsoft/aspnet-api-versioning/blob/e16b579d240574053ebd8e7ee38c8686beaee174/src/Microsoft.AspNetCore.Mvc.Versioning/Versioning/ApiVersionActionSelector.cs#L165

It seems like the ApiVersioning code lives in a separate repo not under the dotnet umbrella which introduces a dependency problem. I wonder if its possible to introduce an extension point for this middleware that allows implementers to add additional functionality to the ClearHttpContext step of the exception middleware. This way, the ApiVersioning folks could add an implementation which clears their feature.

Bottom line, the exception middleware is already clearing the IRouteValuesFeature in order to accomplish its goal because it is assuming that this is the only code that was involved in selecting the original route. However, any libraries which introduce a custom layer of routing, like ApiVersioning, will fail to be cleared properly before re-entering the pipeline. Adding some kind of extension point such that routing libraries can expose the means for others to clear their state, could clear this issue up.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
Tratchercommented, Jun 7, 2021

It does seem reasonable to clear previous selections without reallocating if the API Versioning middleware is entered again.

Ah, that would be here: https://github.com/microsoft/aspnet-api-versioning/blob/e16b579d240574053ebd8e7ee38c8686beaee174/src/Microsoft.AspNetCore.Mvc.Versioning/Versioning/ApiVersioningMiddleware.cs#L16-L22

1reaction
Tratchercommented, Jun 5, 2021

Middleware are supposed to clear their own state on exit. The exception handling middleware clearing the route data was a performance compromise to avoid an extra state machine in the route middleware.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting the api-version in httpcontext for integration tests. ...
It's not clear what this does, but it should simply provide the API version string. If the specified value is invalid, this will...
Read more >
Handle errors in ASP.NET Core web APIs
A problem details response is generated with the previous code when any of the following conditions apply: An invalid input is supplied. The...
Read more >
How to handle errors in minimal APIs in ASP.NET Core
ExceptionHandlerMiddleware : This middleware is used to generate a ProblemDetails response if no custom handler is available.
Read more >
Aspnet Api Versioning Versions
View the latest Aspnet Api Versioning versions.
Read more >
exceptionHandLermIddleware无法清除IAPIRSIONINGFEATURE
ExceptionHandlerMiddleware does not clear IApiVersioningFeature. 我最初在此处在Aspnetcore方面提交了此问题:dotnet/aspnetcore#33302.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found