Search code examples
iis.net-coreasp.net-core-2.2

How to instruct IIS to use environment variable from web.config, to run the .net core application?


Recently, I'm facing a strange issue while hosting .net core 2.2 application on IIS.

After hosting, I get "An error occurred while starting the application". In order to identify the root cause I enabled the log file and found that its because of environment variable issue.

I configured the environment variable in my web.config file as like below,

<aspNetCore processPath="dotnet" arguments=".\PctrClient.Api.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="InProcess">
    <environmentVariables>
      <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
    </environmentVariables>
  </aspNetCore>

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>()
               .UseIISIntegration();
}

But web app interprets it as Development;Development. How is it possible?

Info from log file

info Hosting environment: Development;Development


Solution

  • In order to resolve this issue, we have to set the environment variable either in system level or in web.config file. (if we set the environment variable in both places, it will get appended by .net core 2.2 framework)

    From my perspective, it looks like a strange behavior, so I have raised this issue in GitHub/AspNetCore to know other community users thoughts, hope this issue will get addressed soon.