swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B

使用Web API并使用swashbuckle生成swagger文档,我在两个不同的命名空间中定义了两个具有相同名称的不同类。当我在浏览器中打开swagger页面时,它说

1 Conflicting schemaIds: Duplicate schemaIds detected for types A and B. See the config setting - "UseFullTypeNameInSchemaIds" for a potential workaround

完整消息:

 1 {
 2     "message": "An error has occurred.",
 3     "exceptionMessage": "Conflicting schemaIds: Duplicate schemaIds detected for types Amo.Common.AjaxResult`1[[System.Collections.Generic.List`1[[Amo.UsdtCoin.UsMerchantClient+oListTransactionResult, Amo.UsdtCoin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] and Amo.Common.AjaxResult`1[[System.Collections.Generic.List`1[[Amo.BtcCoin.MerchantClient+oListTransactionResult, Amo.BitCoin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]. See the config setting - "UseFullTypeNameInSchemaIds" for a potential workaround",
 4     "exceptionType": "System.InvalidOperationException",
 5     "stackTrace": " 在 Swashbuckle.Swagger.SchemaRegistry.CreateRefSchema(Type type)

 6      在 Swashbuckle.Swagger.SchemaRegistry.CreateInlineSchema(Type type)
 
 7      在 Swashbuckle.Swagger.SchemaRegistry.GetOrRegister(Type type)
 
 8      在 Swashbuckle.Swagger.SwaggerGenerator.CreateOperation(ApiDescription apiDesc, SchemaRegistry schemaRegistry)
 
 9      在 Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable`1 apiDescriptions, SchemaRegistry schemaRegistry)
 
10      在 Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.<GetSwagger>b__4(IGrouping`2 group)
 
11      在 System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
 
12      在 Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion)
 
13      在 Amo.Client.CachingSwaggerProvider.GetSwagger(String rootUrl, String apiVersion) 位置 E:\codes\USDTSolution\Amo.Client\App_Start\SwaggerConfig.cs:行号 301
 
14      在 Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 
15      在 System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 
16      在 System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 
17      在 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 在 System.Web.Http.HttpServer.<SendAsync>d__24.MoveNext()"
18 }

我不想改变类的名字。我该如何解决?

解决方法1:

1  services.ConfigureSwaggerGen(options =>
2    {
3        //your custom configuration goes here
4 
5 ...
6 
7   // UseFullTypeNameInSchemaIds replacement for .NET Core
8        options.CustomSchemaIds(x => x.FullName);
9    });

解决方法2:

 1 GlobalConfiguration.Configuration 
 2     .EnableSwagger(c =>
 3     {
 4         // your configs...
 5 
 6         c.SchemaId(x => x.FullName);
 7 
 8         // other configs...
 9     })
10     .EnableSwaggerUi(c =>
11         // ....
12     });

解决方法3:

1 c.UseFullTypeNameInSchemaIds();
原文地址:https://www.cnblogs.com/yanglang/p/9475539.html