Skip to content

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

Closed
dreamMoli opened this issue Oct 16, 2020 · 7 comments
Assignees

Comments

@dreamMoli
Copy link

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?

@commonsensesoftware
Copy link
Collaborator

Nothing has changed here. Can you provide some additional context? Stack trace? Repro?

@dreamMoli
Copy link
Author

Stack trace:CLR/System.InvalidOperationException
“System.InvalidOperationException”type in DataWebApi.dll ,thorw error: 'Sequence contains no matching element'
in System.Linq.ThrowHelper.ThrowNoMatchException()
in System.Linq.Enumerable.First[TSource](IEnumerable1 source, Func2 predicate)
in Microsoft.Extensions.DependencyInjection.IServiceCollectionExtensions.WithUrlHelperFactoryDecorator(IServiceCollection services)
in Microsoft.Extensions.DependencyInjection.IServiceCollectionExtensions.AddApiVersioningServices(IServiceCollection services)
in Microsoft.Extensions.DependencyInjection.IServiceCollectionExtensions.AddApiVersioning(IServiceCollection services, Action`1 setupAction)
in DataWebApi.Startup.ConfigureServices(IServiceCollection services) in /Users/gtl/Desktop/NetCoreMac/AirSupplyPlatformServerCore/WebServer/AirSupplyPlatformDataServer/DataWebApi/Startup.cs : lines:53
in System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
in System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
in Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
in Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.g__Startup|0(IServiceCollection serviceCollection)
in Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
in Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.b__0(IServiceCollection services)
in Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services, Object instance)
in Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.b__0(HostBuilderContext context, IServiceCollection services)
in Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
in Microsoft.Extensions.Hosting.HostBuilder.Build()
in $.<

$>d__0.MoveNext() in /Users/gtl/Desktop/NetCoreMac/AirSupplyPlatformServerCore/WebServer/AirSupplyPlatformDataServer/DataWebApi/Program.cs : lines: 12
##############################################
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);
});

@RoySalisbury
Copy link

I'm getting the same error. I think it has to do with not finding the IUrlHelperFactory within the services list.

-> WithUrlHelperFactoryDecorator:
var descriptor = services.First( sd => sd.ServiceType == typeof( IUrlHelperFactory ) );

This is the only line that uses Linq, and "First" will throw that error.

@RoySalisbury
Copy link

It looks like it is depending on the interface IUrlHelperFactory being added in

services.AddMvcCore()
or
services.AddControllersWithView()

Make the call to AddApiVersioning() AFTER you setup the controllers call. That seems to fix it.

Roy

@RoySalisbury
Copy link

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.
-or-
B. Move the AddApiVersioning as an extension to the IMvcBuilder and not IServiceCollection.

I vote for "B". Its cleaner and better organized.

@dreamMoli
Copy link
Author

It looks like it is depending on the interface IUrlHelperFactory being added in

services.AddMvcCore()
or
services.AddControllersWithView()

Make the call to AddApiVersioning() AFTER you setup the controllers call. That seems to fix it.

Roy

Thanks Roy!
For your advice,I try it. finally i found you are right! And I'm for your opinion!

@commonsensesoftware
Copy link
Collaborator

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. FirstOrDefault) or compose ApiVersionUrlHelperFactory and UrlHelperFactory (the default) automatically. All of these types are public so if that doesn't work for someone, they have the ability to change and recompose the registration as they see fit.

The provided workaround is the recommended way to mitigate this for now. The fix is inbound.

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

3 participants