Magnus Karlsson
Magnus Karlsson

Reputation: 3579

Azure App service can't start because it can't find swagger documentation xml using ASP.NET Core

Update 2: The file does not get published. That is what is wrong. However, I can't figure out why one of my computers can publish it the right way and the other doesn't after the Visual studio upgrade.

Update: Running the same solution on two different computers where the APIProject.xml file get's correctly published from one computer but not the other there are now only one difference left. The computer from which the publishing works runs the not updated version of Visual studio 2017 Enterprise. 15.5.2 does not work and 15.4.2 works.

I'm getting this error:

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

The file is placed in bin\Debug\netcoreapp2.0 It works locally but when published to Azure App service it crashes.

I'm publishing to the staging slot I created and haven't tried production yet. Publishing replaces all files at destination.

Here is my setup of Swagger but it used to work :)

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);
        });   
    }

What has changed is I added the "UseSetting"-row in Program.cs to get errors of why it doesn't start. Before adding that row I did't get developer error, only got end user error page.

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

I'm trying to publish with Debug mode. Also tried with Release as of one answer below but no difference.

I checked out the code in another computer and publishing from that computer works so the problem just got more weird to me!

Upvotes: 8

Views: 5348

Answers (4)

Mike Gledhill
Mike Gledhill

Reputation: 29161

My .Net Core 2.0 app worked fine locally, but threw a pointless "An error occurred while starting the application." error when deployed to Azure.

Checking the logs, the cause was that Swagger couldn't find a particular .xml file (why the hell doesn't the error message let us know ?!)

The solution was to select the Build tab, change the Configuration drop down list to "Release", then tick this box:

enter image description here

That's it.

For some reason, by default it was ticked (with the .xml filename filled in) for Debug, but not for Release.

(Sigh.)

Upvotes: 21

ob1dev
ob1dev

Reputation: 1230

I faced the same issue today when published ASP.NET Core Web Application powered by .NET Core 2.1 to Azure App Service.

It worked locally fine when running in Debug mode, but failed in Release. The reason was because I didn’t have the following config section in the *.csproj file.

<Project Sdk="Microsoft.NET.Sdk.Web">
...
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DocumentationFile>bin\Release\netcoreapp2.1\OneGit.Api.xml</DocumentationFile>
  </PropertyGroup>
...
</Project>

Which is represent the checkbox XML documentation file in the section Build => Output when right-click on a project and select menu-item Properties.

Upvotes: 4

Tanver Hasan
Tanver Hasan

Reputation: 1763

Changing publish configuration from release to debug solved my problem. enter image description here

Upvotes: 1

Boris Lipschitz
Boris Lipschitz

Reputation: 9516

Make sure that the equivalent block exists in the csproj file in the Debug as well as the Release mode. I guess you are publishing the release mode.

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DocumentationFile>bin\Release\netcoreapp2.0\APIProject.xml</DocumentationFile>
</PropertyGroup>

Upvotes: 4

Related Questions