【问题标题】:Swashbuckle documentation NullReferenceException on load加载时的 Swashbuckle 文档 NullReferenceException
【发布时间】:2019-04-15 22:09:52
【问题描述】:

我们目前在尝试访问我们的文档时遇到了问题。我们目前在 Webforms Web 应用程序中托管了一个 WebAPI2 项目,该项目对我们来说已经运行了将近一年。我们使用的是 Swashbuckle 5.2.2,但我确实更新到了当前版本,看看它是否能解决这个问题。它并没有神奇地解决这个问题,所以它现在恢复到 5.2.2,因为我们即将发布。

数周前突然尝试打开文档时开始抛出异常。到目前为止,我们能够收集到的唯一信息是输出到浏览器的以下信息:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Object reference not set to an instance of an object.</ExceptionMessage>
<ExceptionType>System.NullReferenceException</ExceptionType>
<StackTrace>   at ᜄ.ᜀ.<>c.ᜀ(Task`1 A_0)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Cors.CorsMessageHandler.<SendAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()
</StackTrace>
</Error>

我们已经确定了它停止工作的构建,并检查了触发构建的代码提交。但是,根本没有对 WebAPI 项目进行任何提交,除了主 web 应用程序中的一些 CSS 之外,也没有任何其他内容不会影响 Swagger 文档。

我们比较了两个版本之间的 Web 应用程序文件夹,除了小的 CSS 差异之外,唯一的区别是重新编译的项目程序集。每个构建都是从源代码控制中全新构建的。

我检查了 WebAPI2 项目程序集,自定义 swagger 资源文件(图像、html 和 css 文件)都正确嵌入到两个版本中。

这是我们的 SwaggerConfig 内容(一些识别名称已更改):

const string webApiXmlDocFileName = "WebAPI2.xml";
const string modelXmlDocFileName = "Core.xml";

GlobalConfiguration.Configuration
    .EnableSwagger("api/docs/{apiVersion}", c =>
    {
        c.IgnoreObsoleteActions();
        c.IgnoreObsoleteProperties();
        c.DescribeAllEnumsAsStrings();
        c.IncludeXmlComments(webApiXmlDocFileName);
        c.IncludeXmlComments(modelXmlDocFileName);
        c.DocumentFilter<AlphabeticSortOperationFilter>();

        c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
        c.SingleApiVersion("v1", "API Reference");

        c.Schemes(new[] { "http", "https" });
        c.OAuth2("oauth2")
            .Description("OAuth2 Implicit Grant")
            .TokenUrl("/api/oauth2/token")
            .Flow("password")
            .Scopes(scopes =>
            {
                scopes.Add("clientid", "try out simple api");
            });
        c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
    })
    .EnableSwaggerUi("api/docs/ui/{*assetPath}", c =>
    {
        // Specify our custom templates isntead of defaults
        c.InjectStylesheet(Assembly.GetExecutingAssembly(), "WebAPI2.SwashbuckleExtensions.index.css");
        c.CustomAsset("index", Assembly.GetExecutingAssembly(), "WebAPI2.SwashbuckleExtensions.index.html");
        c.CustomAsset("logo", Assembly.GetExecutingAssembly(), "WebAPI2.SwashbuckleExtensions.Logo-white.png");
        c.EnableOAuth2Support("clientid", "realm", "project");
        c.DisableValidator();
    });

我们没有在日志中得到任何内容,并且遍历我们的所有代码并不会暴露我们的错误。

有人对如何进一步深入研究有任何建议吗?我们在这里有点不知所措,因为我们无法捕获请求路由中错误的来源。任何帮助将不胜感激。

编辑 我终于找到了更多可能对这个问题有所启发的信息。

我们在我们的应用程序中使用了第 3 方控件,它看起来像是托管自己的 WebAPI 实例,以便处理自己的 RESTful 调用。在 webapp 初始化期间调用他们的特定方法时会发生上述错误。我已经联系了他们,看看是否需要拨打电话,或者不拨打电话可能会产生什么后果。

问题变成了,有没有办法让 Swashbuckle 完全忽略程序集或类?由于它是第 3 方,我们无法对其进行修改以添加自定义属性,并且使用过滤器将不起作用,因为此时已经引发了异常。

【问题讨论】:

标签: c# .net asp.net-web-api2 swashbuckle


【解决方案1】:

不确定这是否是您遇到的情况,但我遇到了相同的 System.NullReferenceException: 'Object reference not set to an instance of an object。有

的行中的异常

foreach(apiExplorer.ApiDescriptions 中的变量组):

c.MultipleApiVersions(
                                (apiDescription, version) => apiDescription.GetGroupName() == version,
                                info =>
                                {
                                foreach (var group in apiExplorer.ApiDescriptions)
                                    {
                                        var description = ....

通过逐行删除我添加的内容,我得出结论,我在 API 控制器操作中的 [Route] 属性中犯了一个错误。 我没有用'/'关闭路线

所以,这没有用

[Route("templateFields/{templateFieldId:int}/inputtype")]

但这有效

[Route("templateFields/{templateFieldId:int}/inputtype/")] - 不是末尾的 /

这导致了异常!

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多