gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:49:18 26 4
gpt4 key购买 nike

从 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.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.)

最佳答案

尝试添加这段代码:

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

opt.SubstituteApiVersionInUrl = true;
});

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com