[英]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. 从visual studio运行我的项目时,我得到以下异常,它是一个ASP.NET核心项目。
It seems that a dependency is registered twice, hence the "System.ArgumentException: An item with the same key has already been added." 似乎依赖项被注册两次,因此“System.ArgumentException:已经添加了具有相同键的项目”。
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. 它似乎也与Api中的Versioning有关,因为如果删除此代码,异常就会消失。
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.
Autofac.Core.DependencyResolutionException:激活特定注册期间发生错误。 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.
注册:Activator = ISwaggerProvider(DelegateActivator),Services = [Swashbuckle.AspNetCore.Swagger.ISwaggerProvider],Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,Sharing = None,Ownership = OwnedByLifetimeScope激活特定注册期间发生错误。 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.
注册:Activator = ApiDescriptionGroupCollectionProvider(ReflectionActivator),Services = [Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider],Lifetime = Autofac.Core.Lifetime.RootScopeLifetime,Sharing = Shared,Ownership = OwnedByLifetimeScope激活特定注册时发生错误。 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
注册: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'.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])'类型'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
到你的
*.csproj
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.