错误: 冲突模式: 检测到 A 和 B 类型的重复模式

使用 Web API 和 swashuckle 生成 swagger 文档, 我在两个不同的名称空间中定义了两个具有相同名称的不同类。当我在浏览器里打开招摇过市的页面时,上面写着

冲突模式: 检测到类型 A 和 B 的重复模式,请参阅配置设置-“ UseFullTypeNameInSchemalds”以获得可能的解决方案

完整信息:

500: {“消息”: “发生了一个错误。”,“ ExceptionMessage”: “冲突模式: 检测到类型 A 和 B 的重复模式。查看配置设置-“ UseFullTypeNameInSchemalds”以获得潜在的解决方案”,“ ExceptionType”: “ System。“ InvalidOperationException”,“ stackTrace”: “在 Swashbuckle。斯威格。SchemaRegistry.Swashbuckle 的 CreateRefSchema (Type Type) r n。斯威格。SchemaRegistry.Swashbuckle 的 CreateinlineSchema (Type Type) r n。斯威格。在系统中的 SchemaRegistry.b _ _ 1f (JsonProperty 道具) r n。Linq.Enumerable.toDictionary [ TSource,tKey,TElement ](iEnumerable1 source, Func2 keySelector,Func2 elementSelector, IEqualityComparer1 comprer) r n at Swashbuckle.斯威格。SchemaRegistry.Swashbuckle 的 CreateObjectSchema (jsonObjectContractjsonContractr n)。斯威格。SchemaRegistry.在 Swashbuckle 创建定义模式(类型类型) r n。斯威格。SchemaRegistry.在 Swashbuckle 的 getorRegister (类型类型) r n。斯威格。斯威格发电机。Swashbuckle 的创建操作(apiDescription apiDesc,SchemaRegistry schema/Registry) r n。斯威格。斯威格发电机。CreatePathItem (IEnumerable1 apiDescriptions, SchemaRegistry schemaRegistry)\r\n at Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.<GetSwagger>b__4(IGrouping2 group) r n at System.Linq.Enumerable.toDictionary [ TSource,tKey,TElement ](iEnumerable1 source, Func2 keySelector,Func2 elementSelector, IEqualityComparer1 comprer) r n at Swashbuckle.斯威格。斯威格发电机。GetSwagger (字符串 rootUrl,字符串 apiVersion) r n at Swashbuckle。申请。斯威格 · 多克斯 · 汉德勒。系统中的 SendAsync (HttpRequestMessage 请求,CcellationToken CancellationToken) r n。网。哈哈。HttpMessageInvoker.系统中的 SendAsync (HttpRequestMessage 请求,CcellationToken CancellationToken) r n。韦伯。哈哈。调度员。HttpRoutingDispatcher.系统中的 SendAsync (HttpRequestMessage 请求,CcellationToken CancellationToken) r n。网。哈哈。授权处理程序。系统中的 SendAsync (HttpRequestMessage 请求,CcellationToken CancellationToken) r n。韦伯。哈哈。HttpServer.d _ _ 0.移动下一步()”} http://localhost:24215/swagger/docs/v1

我不想更改我的课程名称,我该怎么办?

56207 次浏览

I finally found a way in swagger configs. Go to App_Start\SwaggerConfig.cs file and under EnableSwagger lambda expression add this line:

c.SchemaId(x => x.FullName);

Full code is like this:

GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
// your configs...


c.SchemaId(x => x.FullName);


// other configs...
})
.EnableSwaggerUi(c =>
// ....
});

If you comment out or add:

c.UseFullTypeNameInSchemaIds();

In that section, it seems to do same thing.

Every class in the swagger JSON must have a unique schemaId.

Swashbuckler tries to just use the class name as a simple schemaId, however if you have two classes in different namespaces with the same name (as you do) this will not work.

As the error suggests, you can use the config setting "UseFullTypeNameInSchemaIds*" for a potential workaround (Update: not available in recent versions)

In newer versions you can achieve the same behavior via options.CustomSchemaIds(x => x.FullName).

Here is an example:

   services.ConfigureSwaggerGen(options =>
{
//your custom configuration goes here


...


// UseFullTypeNameInSchemaIds replacement for .NET Core
options.CustomSchemaIds(x => x.FullName);
});

for more information http://wegotcode.com/microsoft/swagger-fix-for-dotnetcore/

I am using Asp.net Core 2.1. This error resulted when I tried to show Swagger UI.

I solved the problem this way:

In Starup.cs, in ConfigureServices() add c.CustomSchemaIds(i => i.FullName);

see example below:

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Title = "ASP.NET Core 2.1+ ConsumerApp API",
Version = "v1"
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
c.CustomSchemaIds(i => i.FullName);
});

If your model contains generic types, consider using Type.ToString() instead of Type.FullName to get rid of assembly information generated for generic parameter type and make your schema ids more consistent.

services.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.ToString());
});

Example showing the difference on List<string>:

Console.WriteLine(typeof(List<string>).FullName);
Console.WriteLine(typeof(List<string>).ToString());


// Output:
//    System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
//    System.Collections.Generic.List`1[System.String]

For Swashbuckle.AspNetCore 5.2.1 (on .NET Core 3.1), the Swashbuckle configuration API seems to lack the options described in the older solutions. Instead, the following change in the Startup.cs worked for me:

  services.AddSwaggerGen(c =>
{
// Existing configuration


// Tweak to the Schema generator
c.SchemaGeneratorOptions = new SchemaGeneratorOptions {SchemaIdSelector = type => type.FullName};
}

In my case, I had 2 of the same classes in my 2 microservice projects (A, and B).

When I opened the dependencies folder in project(A), I found that I added the other project(B) as a reference to this project by mistake.

After removing the dependency between my two projects the conflict was solved.