• 【NSwag】System.InvalidOperationException错误


    原地址

    https://github.com/RicoSuter/NSwag/issues/3584

    环境

    ASP.NETCore:3.1

    NSwag.AspNetCore:13.13.0

    引发错误

    System.InvalidOperationException: Method may only be called on a Type for which Type.IsGenericParameter is true.
       at System.RuntimeType.get_GenericParameterPosition()
       at Namotion.Reflection.ContextualType.<get_Properties>b__42_0(PropertyInfo property)
       at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at Namotion.Reflection.ContextualType.get_Properties()
       at Namotion.Reflection.ContextualTypeExtensions.GetContextualProperties(Type type)
       at NJsonSchema.Generation.JsonSchemaGenerator.ApplyAdditionalProperties[TSchemaType](TSchemaType schema, Type type, JsonSchemaResolver schemaResolver)
       at NJsonSchema.Generation.JsonSchemaGenerator.GenerateObject(JsonSchema schema, JsonTypeDescription typeDescription, JsonSchemaResolver schemaResolver)
       at NSwag.Generation.OpenApiSchemaGenerator.GenerateObject(JsonSchema schema, JsonTypeDescription typeDescription, JsonSchemaResolver schemaResolver)
       at NJsonSchema.Generation.JsonSchemaGenerator.Generate[TSchemaType](TSchemaType schema, ContextualType contextualType, JsonSchemaResolver schemaResolver)
       at NJsonSchema.Generation.JsonSchemaGenerator.Generate[TSchemaType](ContextualType contextualType, JsonSchemaResolver schemaResolver)
       at NJsonSchema.Generation.JsonSchemaGenerator.GenerateWithReferenceAndNullability[TSchemaType](ContextualType contextualType, Boolean isNullable, JsonSchemaResolver schemaResolver, Action`2 transformation)
       at NSwag.Generation.OpenApiSchemaGenerator.GenerateWithReferenceAndNullability[TSchemaType](ContextualType contextualType, Boolean isNullable, JsonSchemaResolver schemaResolver, Action`2 transformation)
       at NSwag.Generation.AspNetCore.Processors.OperationResponseProcessor.Process(OperationProcessorContext operationProcessorContext)
       at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.RunOperationProcessors(OpenApiDocument document, ApiDescription apiDescription, Type controllerType, MethodInfo methodInfo, OpenApiOperationDescription operationDescription, List`1 allOperations, OpenApiDocumentGenerator swaggerGenerator, OpenApiSchemaResolver schemaResolver)
       at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.AddOperationDescriptionsToDocument(OpenApiDocument document, Type controllerType, List`1 operations, OpenApiDocumentGenerator swaggerGenerator, OpenApiSchemaResolver schemaResolver)
       at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.GenerateForControllers(OpenApiDocument document, IGrouping`2[] apiGroups, OpenApiSchemaResolver schemaResolver)
       at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.GenerateAsync(ApiDescriptionGroupCollection apiDescriptionGroups)
       at NSwag.AspNetCore.Middlewares.OpenApiDocumentMiddleware.GenerateDocumentAsync(HttpContext context)
       at NSwag.AspNetCore.Middlewares.OpenApiDocumentMiddleware.GetDocumentAsync(HttpContext context)
       at NSwag.AspNetCore.Middlewares.OpenApiDocumentMiddleware.Invoke(HttpContext context)
       at NSwag.AspNetCore.Middlewares.SwaggerUiIndexMiddleware.Invoke(HttpContext context)
       at NSwag.AspNetCore.Middlewares.RedirectToIndexMiddleware.Invoke(HttpContext context)
       at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

    处理方案

    将NSwag.AspNetCore类库升级v13.13.1

    问题描述

    修改前

    <PackageReference Include="NSwag.AspNetCore" Version="13.13.0" />

    发布后无法打开,而且本地打开也报错

    (1)注释了所有的Controller下面的方法

    然后Swagger页面可以打开了,只是页面没有可用的接口

     (2)结合报错提示Type,想到了MessageModel类有个泛型的T Data属性,随记更换了返回值

    现在Swagger也运行正常,而且展示把Action也展示出来了

     (3)确定了是由于MessageModel泛型引发了异常,但是具体的细节就没有去看了

    修改后

    <PackageReference Include="NSwag.AspNetCore" Version="13.13.1" />

    仍然是返回MessageModel,运行正常,所以还是NSwag类库的问题

  • 相关阅读:
    Linux 开机启动 php socket
    Linux 判断进程是否运行
    应用容器公共免费部署平台
    dos2unix 批量转化文件
    django中多个app放入同一文件夹apps
    django无法同步mysql数据库 Error:1064
    cos migration工具webhook推送
    腾讯云 COS 对象存储使用
    docker nginx 运行后无法访问
    makefile中 = := += 的区别
  • 原文地址:https://www.cnblogs.com/masonblog/p/15622930.html
Copyright © 2020-2023  润新知