Search code examples
c#azureasp.net-coreweb-config

Visual Studio 2019 IIS profile creates web.config file that breaks Azure App Service


When creating a new Debug profile with Microsoft Visual Studio Community 2019 Version 16.6.3 on a ASP.NET Core Web 3.1 project a web.config file is created.

enter image description here

The file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="C:\Users\<USER>\Source\Project\Project.Web\bin\Debug\netcoreapp3.1\Project.Web.exe" arguments="" stdoutLogEnabled="false" hostingModel="InProcess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

Errors present when accessing the site with web.config:

AggregateException: One or more errors occurred. (One or more errors occurred. (The NPM script 'start' exited without indicating that the create-react-app server was listening for requests. The error output was: )) System.Threading.Tasks.Task.ThrowIfExceptional(bool includeTaskCanceledExceptions)

InvalidOperationException: The NPM script 'start' exited without indicating that the create-react-app server was listening for requests. The error output was: Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer.ReactDevelopmentServerMiddleware.StartCreateReactAppServerAsync(string sourcePath, string npmScriptName, ILogger logger)


Solution

  • I first thought that the exact aspNetCore processPath was the culprit but given the error it more looked like code from Startup.cs.

    if (env.IsDevelopment())
    {
        spa.UseReactDevelopmentServer(npmScript: "start");
    }
    

    Looking at the actual file deployed it confirmed my suspicions, the path was relative here.

    enter image description here

    I needed to set ASPNETCORE_ENVIRONMENT as Production. Solved this by creating a new web.release.config file since I needed to test the application on an actual IIS with ASPNETCORE_ENVIRONMENT as Development. With this transformation everything worked:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    
      <!-- To customize the asp.net core module uncomment and edit the following section. 
      For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
      <!--
      <system.webServer>
        <handlers>
          <remove name="aspNetCore"/>
          <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
        </handlers>
        <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
      </system.webServer>
      -->
    
      <location>
        <system.webServer>
          <aspNetCore>
            <environmentVariables>
              <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" xdt:Locator="Match(name)" xdt:Transform="SetAttributes" />
            </environmentVariables>
          </aspNetCore>
        </system.webServer>
      </location>
    
    </configuration>
    

    Source:

    https://stackoverflow.com/a/54702411/3850405