我创建了如下所示的类。我的项目中有类似的类,除此类外,没有其他类会导致错误。我在类中找不到任何问题,但它正在导致异常,在我的 description 变量中, ModelMetadata 属性为null,这是导致错误的原因。
这是在我的SwaggerDefaultValues.cs
中定义 description 变量的方式var description = context.ApiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name);
并在此处引发异常,ModelMetadata为null,这是导致System.NullReferenceException的原因:
parameter.Description = description.ModelMetadata.Description;
在我跟踪代码时,此类定义导致问题,
CardPlusCNumberUpdateQueryParameter类:
using System;
using System.ComponentModel.DataAnnotations;
namespace WestCore.AppCore.Services.PCsbins.Customer
{
public class CardPlusCNumberUpdateQueryParameter
{
/// <summary>
/// Customer Number
/// </summary>
[Range(1, 999999)]
[Required]
public int CustomerNumber { get; set; }
/// <summary>
/// CardPlus Customer Number
/// </summary>
[Required]
public string CardPlusCustomerNumber { get; set; }
}
}
StackTrace:
at WestCore.Api.Swagger.SwaggerDefaultValues.Apply(Operation operation, OperationFilterContext context) in C:\Users\can.pahali\source\WestCoreApiSS\Main\WestCore.Api\Swagger\SwaggerDefaultValues.cs:line 38
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable`1 apiDescriptions, ISchemaRegistry schemaRegistry)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.<>c__DisplayClass6_0.<CreatePathItems>b__2(IGrouping`2 group)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable`1 apiDescriptions, ISchemaRegistry schemaRegistry)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath, String[] schemes)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
更新:我还为context.ApiDescription.ParameterDescriptions对象添加了屏幕截图。