Aspnetcore: Duplicate ASPNETCORE_Environment present for InProcess hosting causes ASPNET CORE 2.2 fails to load appsettings

Created on 11 Dec 2018  路  9Comments  路  Source: dotnet/aspnetcore

After 2 days trying to figure out, i think i discovered a bug:

1) I already had set manually the ASPNETCORE_ENVIRONMENT on Windows Server 2016 environment variables.
2) After 2.2 release, i decided to update the publish profile .pubxml to include the desired environment, for instance:

<EnvironmentName>Development</EnvironmentName>

This generates the web.config:

<aspNetCore processPath="dotnet" arguments=".\TJPA.CentralBoletos.API.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
   <environmentVariables>
           <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
   </environmentVariables>
</aspNetCore>

3) When i published the application, the application wont startup, specifically when reading the
connection string for the HangfireDB schema i was using.

In Web browser says:

An error occurred while starting the application.
.NET Core 4.6.27110.04 X64 v4.0.0.0 | Microsoft.AspNetCore.Hosting version 2.2.0-rtm-35687 | Microsoft Windows 10.0.14393 | Need help?

In std logs:

Application startup exception: System.ArgumentNullException: Value cannot be null.
Parameter name: nameOrConnectionString
at Hangfire.SqlServerStorageExtensions.UseSqlServerStorage(IGlobalConfiguration configuration, String nameOrConnectionString, SqlServerStorageOptions options)
at TJPA.CentralBoletos.API.Startup.<>c__DisplayClass5_0.b__2(IGlobalConfiguration config) in C:\GIT\central-boletosapi-central-boletos\TJPA.CentralBoletos.API\Startup.cs:line 52
at Hangfire.HangfireServiceCollectionExtensions.<>c__DisplayClass0_0.b__9(IServiceProvider serviceProvider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetServiceT
at Hangfire.HangfireApplicationBuilderExtensions.ThrowIfNotConfigured(IApplicationBuilder app)
at Hangfire.HangfireApplicationBuilderExtensions.UseHangfireServer(IApplicationBuilder app, BackgroundJobServerOptions options, IEnumerable`1 additionalProcesses, JobStorage storage)
at TJPA.CentralBoletos.API.Startup.ConfigurarHangfireServer(IApplicationBuilder app, IServiceProvider serviceProvider) in C:\GIT\central-boletosapi-central-boletos\TJPA.CentralBoletos.API\Startup.cs:line 149
at TJPA.CentralBoletos.API.Startup.Configure(IApplicationBuilder app, IServiceProvider serviceProvider) in C:\GIT\central-boletosapi-central-boletos\TJPA.CentralBoletos.API\Startup.cs:line 79
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
Hosting environment: Development;Development
Content root path: C:\inetpub\wwwroot\central-boletos
Application started. Press Ctrl+C to shut down.

The ancm log:

[aspnetcorev2.dll] Initializing logs for 'C:\Program Files\IIS\Asp.Net Core Module\V2aspnetcorev2.dll'. Process Id: 10784.. File Version: 12.2.18316.0. Description: IIS ASP.NET Core Module V2. Commit: ce8cf65589734f82b0536c543aba5bd60d0a5a98.
[aspnetcorev2.dll] Resolving hostfxr parameters for application: 'dotnet' arguments: 'TJPA.CentralBoletos.API.dll' path: 'C:\inetpub\wwwroot\central-boletos\'
[aspnetcorev2.dll] Known dotnet.exe location: ''
[aspnetcorev2.dll] Process path 'dotnet.exe' is dotnet, treating application as portable
[aspnetcorev2.dll] Resolving absolute path to dotnet.exe from 'dotnet.exe'
[aspnetcorev2.dll] Invoking where.exe to find dotnet.exe
[aspnetcorev2.dll] where.exe invocation returned: 'C:\Program Files\dotnet\dotnet.exe
C:\Program Files (x86)\dotnet\dotnet.exe
'
[aspnetcorev2.dll] Current process bitness type detected as isX64=1
[aspnetcorev2.dll] Processing entry 'C:\Program Files\dotnet\dotnet.exe'
[aspnetcorev2.dll] Binary type 6
[aspnetcorev2.dll] Found dotnet.exe via where.exe invocation at 'C:\Program Files\dotnet\dotnet.exe'
[aspnetcorev2.dll] Resolving absolute path to hostfxr.dll from 'C:\Program Files\dotnet\dotnet.exe'
[aspnetcorev2.dll] hostfxr.dll located at 'C:\Program Files\dotnet\host\fxr\2.2.0\hostfxr.dll'
[aspnetcorev2.dll] Converted argument 'TJPA.CentralBoletos.API.dll' to 'C:\inetpub\wwwroot\central-boletos\TJPA.CentralBoletos.API.dll'
[aspnetcorev2.dll] Parsed hostfxr options: dotnet location: 'C:\Program Files\dotnet\dotnet.exe' hostfxr path: 'C:\Program Files\dotnet\host\fxr\2.2.0\hostfxr.dll' arguments:
[aspnetcorev2.dll] Argument[0] = 'C:\Program Files\dotnet\dotnet.exe'
[aspnetcorev2.dll] Argument[1] = 'C:\inetpub\wwwroot\central-boletos\TJPA.CentralBoletos.API.dll'
[aspnetcorev2.dll] c:\b\w\e37dd45d8cd1eaf4\src\iisintegration\srcaspnetcoremodulev2\commonlib\fileoutputmanager.cpp:142 Operation failed with LastError: 32 HR: 0x80070020
[aspnetcorev2.dll] Loading request handler: 'C:\inetpub\wwwroot\central-boletos\runtimes\win-x64\nativeassets\netcoreapp2.2aspnetcorev2_inprocess.dll'
[aspnetcorev2.dll] Creating handler application
[aspnetcorev2_inprocess.dll] Initializing logs for 'C:\inetpub\wwwroot\central-boletos\runtimes\win-x64\nativeassets\netcoreapp2.2aspnetcorev2_inprocess.dll'. Process Id: 10784.. File Version: 12.2.18316.0. Description: IIS ASP.NET Core Module V2 Request Handler. Commit: ce8cf65589734f82b0536c543aba5bd60d0a5a98.
[aspnetcorev2_inprocess.dll] Waiting for initialization
[aspnetcorev2_inprocess.dll] Starting in-process worker thread
[aspnetcorev2_inprocess.dll] Resolving hostfxr parameters for application: 'dotnet' arguments: 'TJPA.CentralBoletos.API.dll' path: 'C:\inetpub\wwwroot\central-boletos\'
[aspnetcorev2_inprocess.dll] Known dotnet.exe location: 'C:\Program Files\dotnet\dotnet.exe'
[aspnetcorev2_inprocess.dll] Process path 'dotnet.exe' is dotnet, treating application as portable
[aspnetcorev2_inprocess.dll] Resolving absolute path to hostfxr.dll from 'C:\Program Files\dotnet\dotnet.exe'
[aspnetcorev2_inprocess.dll] hostfxr.dll located at 'C:\Program Files\dotnet\host\fxr\2.2.0\hostfxr.dll'
[aspnetcorev2_inprocess.dll] Converted argument 'TJPA.CentralBoletos.API.dll' to 'C:\inetpub\wwwroot\central-boletos\TJPA.CentralBoletos.API.dll'
[aspnetcorev2_inprocess.dll] Parsed hostfxr options: dotnet location: 'C:\Program Files\dotnet\dotnet.exe' hostfxr path: 'C:\Program Files\dotnet\host\fxr\2.2.0\hostfxr.dll' arguments:
[aspnetcorev2_inprocess.dll] Argument[0] = 'C:\Program Files\dotnet\dotnet.exe'
[aspnetcorev2_inprocess.dll] Argument[1] = 'C:\inetpub\wwwroot\central-boletos\TJPA.CentralBoletos.API.dll'
[aspnetcorev2_inprocess.dll] In-process callbacks set
[aspnetcorev2_inprocess.dll] Event Log: 'Application 'C:\inetpub\wwwroot\central-boletos\' started the coreclr in-process successfully.'
End Event Log Message.
[aspnetcorev2_inprocess.dll] Starting app_offline monitoring in application 'C:\inetpub\wwwroot\central-boletos\'
[aspnetcorev2_inprocess.dll] Starting file watcher thread

When i removed the lines in web.config solved the problem:

   <environmentVariables>
           <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
   </environmentVariables>
area-servers bug servers-iis

Most helpful comment

By default, running a site in Visual Studio in IIS Express will add an ASPNETCORE_ENVIRONMENT variable set to Development in web.config if a web.config exists in the project folder. When the project gets published that environment variable will still be there in web.config. So if web.config overrides other environmental variables then that would be a problem. But maybe the problem is with IIS Express adding that variable.

All 9 comments

I've also encountered this issue (ironically, also due to an environment-specific Hangfire DB connection string) when trying the InProcess hosting model. It appears that, when InProcess, the System.Environment.GetEnvironmentVariable(string) call will return multiple environment variable values, delimited by a ;. I assume this is the result of defining the environment variable name in multiple scopes (e.g. Machine-wide and at the IIS site level). However, I haven't been able to prove that out. We generally define the ASPNETCORE_ENVIRONMENT variable at the IIS Site's web.config.

I haven't been able to find any documentation that indicates this as a change in the behavior of the method.

If there are multiple, different, configurations specified, then fair enough it combining them into a semicolon delimited list. But in my case I'm ending up with "Development;Development". I think it should at least avoid such duplication.

Or perhaps to prevent breaking existing apps you could have env.Environment just return the first or last environment specified, and add a new env.Environments (plural) that can contain more than one.

This is a bug, web config should override global environment variable values.

By default, running a site in Visual Studio in IIS Express will add an ASPNETCORE_ENVIRONMENT variable set to Development in web.config if a web.config exists in the project folder. When the project gets published that environment variable will still be there in web.config. So if web.config overrides other environmental variables then that would be a problem. But maybe the problem is with IIS Express adding that variable.

What is the fix for that issue ? I am having the exact some problem. Using the last version of ASP.Net Core Host Bundle

As a workaround, you'd set the ASPNETCORE_ENVIRONMENT variable as either in OS environment variables or in your configuration files. Having it defined in more than one location would cause the behavior as decried above.

Just to save someone else from going slightly mad due to this.

I also experienced this exact error, but not due to environment variables in the project web.config, but instead because I was using a virtual directory in the IIS express App URL. ( configured in the .vs folders applicationhost.config)

I had same issue today on production with .Net Core 2.2.6 InProcess Mode

Production servers defined ASPNETCORE_ENVIRONMENT (Machine Level) > Production and Webconfig also has ASPNETCORE_ENVIRONMENT > Production variable.

All applications couldn't boot because Production;Production environment couldn't be found.
Why it merges environment variable instead of overwrite ?
Is it possible to start application with multiple environment variable ?

I have the exact same issue. My Environment variable is "Development;Development" , and when I change nothing but in package manager console, when I set my env variable to "Production" then my environment variable sets to "Production;Development"... How can I fix this.. Please help.

Was this page helpful?
0 / 5 - 0 ratings