Skip to content

Clear IApiVersioningFeature.SelectionResult On Reentry #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
commonsensesoftware opened this issue Jun 7, 2021 · 0 comments
Closed

Clear IApiVersioningFeature.SelectionResult On Reentry #749

commonsensesoftware opened this issue Jun 7, 2021 · 0 comments

Comments

@commonsensesoftware
Copy link
Collaborator

Background

The IApiVersioningFeature is only allocated and assigned once per request. The original expectation was that the middleware would only be entered a single time and if re-entered, it should be idempotent. When the legacy routing system (e.g. IRouter) is used with API Versioning in ASP.NET Core, the IApiVersioningFeature.SelectionResult becomes stateful for the entire request. If the pipeline, and hence all the middleware, runs more than once, the IApiVersioningFeature.SelectionResult should be cleared or otherwise reset. Failure to do so can result in the incorrect action being selected at the end. This could also result in an ambiguous match result.

Enhancement

Clear any previous values in IApiVersioningFeature.SelectionResult each time the middleware is entered.

Workaround

This problem can currently be worked around by explicitly reinitializing the IApiVersioningFeature instance with custom middleware as follows:

app.Use((context, next) =>
    {
        context.Features.Set<IApiVersioningFeature>(new ApiVersioningFeature(context));
        return next(context);
    });

NOTE: This only applies to the legacy routing system. Endpoint Routing is unaffected by this because it does not use IApiVersioningFeature.SelectionResult.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant