Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core: AddEnvironmentVariables doesn't load variables

I have an asp.net core application (.NET Core 2.1). There is a code in ConfigureServices method in Startup class:

Configuration = new ConfigurationBuilder()
    .SetBasePath(_hostingEnvironment.ContentRootPath)
    .AddJsonFile("appsettings.json", false, true)
    .AddJsonFile($"appsettings.{_hostingEnvironment.EnvironmentName}.json", false, true)
    .AddEnvironmentVariables("MyApp:")
    .Build(); 

Also I set system environment variable MyApp:DumpFolder to override DumpFolder setting in appsettings.json. And here I faced with strange behavior:

  1. If I run the application from visual studio with F5 - it can't see my system environment variable
  2. If I build and run it from bin/Debug folder with dotnet MyApp.dll - it loads variables properly.

I inspected asp.net core and see it uses Environment.GetEnvironmentVariables() method which by default (without specified EnvironmentVariableTarget) retreives variables from current process. I don't understand why variables aren't loaded when I run the application from visual studio?

like image 985
mtkachenko Avatar asked Dec 20 '18 14:12

mtkachenko


1 Answers

Restart your Visual Studio.

You probably just declared your Environment Variables hence Visual Studio does not see them.

like image 77
Derviş Kayımbaşıoğlu Avatar answered Sep 21 '22 06:09

Derviş Kayımbaşıoğlu