c# - 带有 api 版本控制的 Autofac 和 swagger 给出了异常

标签 c# .net asp.net-core swagger autofac

从 visual studio 运行我的项目时出现以下异常,它是一个 ASP.NET Core 项目。

似乎依赖项被注册了两次,因此出现“System.ArgumentException:已添加具有相同键的项目。”

貌似和路由也有关系,但是我找不到调试的方法。

它似乎也与 Api 中的版本控制有关,因为如果我删除这段代码,异常就会消失。

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

任何人都见过这个或知道如何调试它。

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.IEnumerable1[[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.IOptions1[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.)

最佳答案

尝试添加这段代码:

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

                    opt.SubstituteApiVersionInUrl = true;
                });

关于c# - 带有 api 版本控制的 Autofac 和 swagger 给出了异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50681495/

相关文章:

c# - 将 mongodb ObjectId 设置为 web api 2.0 上的 url 参数

c# - C#有效语法(IF语句中的变量声明,不带括号)

.net - 套接字异常 : No such host is known

c# - Entity Framework 的 Repository + UnitOfWork 模式

c# - Active Directory redirect_uri 显示 IP 而不是 DNS 名称。如何在使用 Azure AD 进行身份验证时在代码中提供绝对 CallbackPath?

c# - 扩展另一个应用程序的标题栏

c# - 嵌套枚举和属性命名冲突

model-view-controller - 在自定义中间件中访问 TempData

validation - 如何在 DNX Core 5.0 (ASP.NET 5) 中针对 XSD(单独文档)验证 XML?

asp.net-mvc - 从 ASP.NET MVC 捆绑迁移到 Grunt/Bower?