Azure 应用服务无法启动,因为它无法使用 ASP.NET Core 找到 swagger 文档 xml

标签 azure asp.net-web-api visual-studio-2017 .net-core azure-web-app-service

更新 2: 该文件未发布。这就是问题所在。但是,我不明白为什么我的一台计算机可以以正确的方式发布它,而另一台计算机在 Visual Studio 升级后却不能。

更新:在两台不同的计算机上运行相同的解决方案,其中一台计算机可以正确发布 APIProject.xml 文件,而另一台计算机则不能,现在只剩下一个差异。进行发布的计算机运行未更新版本的 Visual studio 2017 Enterprise。 15.5.2 不起作用,15.4.2 起作用。

我收到此错误:

FileNotFoundException: Could not find file 'D:\home\site\wwwroot\APIProject.xml'.

文件放置在bin\Debug\netcoreapp2.0中 它可以在本地运行,但是当发布到 Azure 应用服务时它会崩溃。

我正在发布到我创建的暂存槽,但尚未尝试生产。发布将替换目标处的所有文件。

这是我的 Swagger 设置,但它曾经有效:)

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        // Register the Swagger generator, defining one or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "Ecommerce/Product API",
                Description = "Handles API based shopping functionality",
                TermsOfService = "Terms of service should be contracted with inidividually",
                Contact = new Contact { Name = "Magnus", Email = "magnus@secret", Url = "" },
                License = new License { Name = "Use under permission from our Group", Url = "http://aboutno" }
            });

            // Set the comments path for the Swagger JSON and UI.
            var basePath = PlatformServices.Default.Application.ApplicationBasePath;
            var xmlPath = Path.Combine(basePath, "APIProject.xml");
            c.IncludeXmlComments(xmlPath);
        });   
    }

发生的变化是我在 Program.cs 中添加了“UseSetting”行以获取有关其无法启动原因的错误。在添加该行之前,我没有收到开发人员错误,只收到最终用户错误页面。

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseApplicationInsights()
            .UseSetting(WebHostDefaults.DetailedErrorsKey, "true")
            .UseStartup<Startup>()
            .Build();

我正在尝试使用 Debug模式发布。还尝试使用 Release 作为下面的一个答案,但没有区别。

我检查了另一台计算机中的代码,并且从该计算机上进行了发布,因此问题对我来说变得更加奇怪!

最佳答案

我的.Net Core 2.0应用程序在本地运行良好,但在部署到Azure时抛出毫无意义的“启动应用程序时发生错误。”错误。

检查日志,原因是 Swagger 找不到特定的 .xml 文件(到底为什么错误消息不让我们知道?!)

解决方案是选择“构建”选项卡,将“配置”下拉列表更改为“发布”,然后勾选此框:

enter image description here

就是这样。

由于某种原因,默认情况下为“调试”勾选了(填写了 .xml 文件名),但没有为“发布”勾选。

(叹气。)

关于Azure 应用服务无法启动,因为它无法使用 ASP.NET Core 找到 swagger 文档 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47919553/

相关文章:

Azure 认知搜索 int 到 Edm.String 字段问题

c# - 使用 WebApi ( $inlinecount ) 的 OData 分页

crash - Visual Studio 2017 启动时崩溃

visual-studio - Visual Studio 2017 工具提示颜色问题

c++ - MFC对话窗口不出现

c# - Azure KeyVault 访问明显延迟

sql - 如何将数据从一个 Azure 数据库表复制到另一个 Azure 数据库表并转换数据类型?

azure - 是否可以在 Kusto 中使用 bag_unpack 解析 Azure LogAnalytics 中的无界 JSON 嵌套?

jquery - 如何将 ASP.NET Core WebAPI 方法绑定(bind)到模型的 bool 字段?

c# - 如何在 Web api 项目中创建页面?