简体   繁体   English

使用api版本控制的Autofac和swagger给出了例外

[英]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.

相关问题 Swagger UI 中的 API 版本控制 - API versioning in Swagger UI 适用于autofac的api异常过滤器 - Api Exception filters for autofac 带有醒目的版本控制的Web API,未找到ResolveVersionSupportByRouteConstraint - web api with swagger versioning, ResolveVersionSupportByRouteConstraint not found .Net Core 3.1 swagger API 版本冲突命名空间 url - .Net Core 3.1 swagger API versioning conflicting namespaces url 具有多个端点的 Swagger 文档中的 RESTful api 版本控制和分组 - RESTful api versioning and grouping in doc from Swagger with multiple endpoints ASP Web API和Autofac集成异常 - ASP Web API and Autofac Integration Exception Swagger UI - 使用 Microsoft.AspNet.WebApi.Versioning 时的 Web API 版本控制 - Swagger UI - Web API versioning when using Microsoft.AspNet.WebApi.Versioning Swagger.NET MVC Api 异常 - Swagger.NET MVC Api Exception 在对我的API进行版本控制时,如果使用相同的DTO,如何维护swagger文档? - When versioning my API, how do I maintain swagger documentation if I use the same DTO? 在 asp.net core 3.1 web api 中,分组和版本控制不能很好地协同工作 - Grouping and Versioning not working well together in swagger in asp.net core 3.1 web api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM