简体   繁体   中英

Autofac and swagger with api versioning gives exception

I get the exception below when running my project from visual studio, it is an ASP.NET Core project.

It seems that a dependency is registered twice, hence the "System.ArgumentException: An item with the same key has already been added."

It also seems like it has something to do with routes, but I can't find a way to debug it.

It also seems to have something to do with Versioning in Api, because if I remove this code, the exception goes away.

services.AddApiVersioning(o =>
        {
            o.AssumeDefaultVersionWhenUnspecified = true;
            o.DefaultApiVersion = new ApiVersion(1, 0);
            o.ReportApiVersions = true;
            o.ApiVersionSelector = new CurrentImplementationApiVersionSelector(o);
        });

Anyone ever seen this or have an idea for how to debug it.

Autofac.Core.DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = ISwaggerProvider (DelegateActivator), Services = [Swashbuckle.AspNetCore.Swagger.ISwaggerProvider], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = ApiDescriptionGroupCollectionProvider (ReflectionActivator), Services = [Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider], Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = IApiDescriptionProvider[] (DelegateActivator), Services = [System.Collections.Generic.IEnumerable 1[[Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider, Microsoft.AspNetCore.Mvc.Abstractions, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = ExternallyOwned An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DefaultApiDescriptionProvider (ReflectionActivator), Services = [Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DefaultInlineConstraintResolver (ReflectionActivator), Services = [Microsoft.AspNetCore.Routing.IInlineConstraintResolver], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptions 1[[Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider, Microsoft.AspNetCore.Mvc.Abstractions, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = ExternallyOwned An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DefaultApiDescriptionProvider (ReflectionActivator), Services = [Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DefaultInlineConstraintResolver (ReflectionActivator), Services = [Microsoft.AspNetCore.Routing.IInlineConstraintResolver], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptions 1[[Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider, Microsoft.AspNetCore.Mvc.Abstractions, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = ExternallyOwned An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DefaultApiDescriptionProvider (ReflectionActivator), Services = [Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DefaultInlineConstraintResolver (ReflectionActivator), Services = [Microsoft.AspNetCore.Routing.IInlineConstraintResolver], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptions 1[Microsoft.AspNetCore.Routing.RouteOptions])' on type 'DefaultInlineConstraintResolver'. An item with the same key has already been added. (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.)

Try adding this code:

services.AddMvcCore().AddVersionedApiExplorer(
                opt =>
                {
                    opt.GroupNameFormat = "'v'VVV";

                    opt.SubstituteApiVersionInUrl = true;
                });

You should be able to fix this by adding,

<GenerateDocumentationFile>true</GenerateDocumentationFile>

to your *.csproj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM