-
Notifications
You must be signed in to change notification settings - Fork 711
Microsoft.AspNetCore.Mvc.Versioning-5.0.0-preview.1 throw Sequence contains no matching element #673
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
Comments
Nothing has changed here. Can you provide some additional context? Stack trace? Repro? |
Stack trace:CLR/System.InvalidOperationException ############################################## Startup.cs : lines:53 code:services.AddApiVersioning(option =>{ option.ReportApiVersions = true; option.ApiVersionReader = new HeaderApiVersionReader("api-version"); option.AssumeDefaultVersionWhenUnspecified = true; option.DefaultApiVersion = new ApiVersion(1,0); }); |
I'm getting the same error. I think it has to do with not finding the IUrlHelperFactory within the services list. -> WithUrlHelperFactoryDecorator: This is the only line that uses Linq, and "First" will throw that error. |
It looks like it is depending on the interface IUrlHelperFactory being added in services.AddMvcCore() Make the call to AddApiVersioning() AFTER you setup the controllers call. That seems to fix it. Roy |
This is going to be a breaking change it appears. The unit tests even appear to have had the "AddMvcCore" added to get them to pass. the extension method "AddApiVersioning" should either: A. Check for and add the necessary interfaces that it will require. I vote for "B". Its cleaner and better organized. |
Thanks Roy! |
I took a little vacation from the pandemic last week. Thanks for trying out the preview and reporting this issue. It is definitely a bug. There should not be any hard requirements in the order for registration. Adding the core MVC services first always seems logical to me, but it shouldn't have to be that way. The crux of this issue is that when people version by URL segment they expect the API version parameter to be magically included in the parameters of route templates. ASP.NET Core only provides automatic values for a few well-known parameters. The simplest solution is just include the parameter when building URLs. However, as this hangs up a lot of people, I've figured out and added a new feature to handle this scenario. To achieve it, it requires decorating rather than replacing the default IUrlHelperFactory. Unfortunately, the DI system doesn't have a clear and supported way to support deferred service replacement for things such as decoration. The IUrlHelperFactory is the only core MVC service that requires this behavior and only if you're versioning by URL segment. The solution will simply be to decorate the IUrlHelperFactory if registered in services (e.g. The provided workaround is the recommended way to mitigate this for now. The fix is inbound. |
i used
configuration:
services.AddApiVersioning(option =>{
option.ReportApiVersions = true;
option.ApiVersionReader = new MediaTypeApiVersionReader("api-version");
option.AssumeDefaultVersionWhenUnspecified = true;
option.DefaultApiVersion = new ApiVersion(1,0);
});
but it throw Sequence contains no matching element!
why and How to deal with it?
The text was updated successfully, but these errors were encountered: