0

I'm writing a .Net Core C# console program. When testing, it was convenient to use the Environment Variables as defined in the project properties:

enter image description here

And I wrote simple code like this:

string rdsDatabase = System.Environment.GetEnvironmentVariable("RDSDatabase");

I just learned there is no .exe created, and you have to use "DotNet" to run the .net core console program (https://blogs.msdn.microsoft.com/benjaminperkins/2017/03/07/net-core-application-where-is-my-exe-how-to-publish/). In that scenario, where do I put those environmental variables at run time? I know there are alternatives such as described in this article: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration?tabs=basicconfiguration. Can I keep it simple and use the GetEnvironmentVariable?

NealWalters
  • 17,197
  • 42
  • 141
  • 251
  • Can you use app config file? – Yuri S Nov 06 '17 at 16:33
  • Create a CMD-file that first sets the variables and then calls dotnet with your dll. But preferably use a configuration file. – Styxxy Nov 06 '17 at 16:34
  • @Yuri - I "can" do anything, but looking for something quick and simple (while balancing best practices). The question is can I use GetEnviornmentVariable? does it read the app.config, and if so, what is the format? – NealWalters Nov 06 '17 at 16:34
  • @Styxxy - use the set command to create windows environment variables then? – NealWalters Nov 06 '17 at 16:35
  • `dotnet publish --self-contained` should get you an exe but I do not know if it will bundle the env variables – M Y Nov 06 '17 at 16:38
  • 2
    Environment variables are kind of a big deal on Unix. The user is expected to bang in his own bash script to do stuff. Since .NETCore is expected to run on one of the Unix flavors, environment variables are back with a vengeance and this config dialog had to be added. That they are not exactly very popular on Windows is how this question happened I guess. Write a .bat file with the SET command. – Hans Passant Nov 06 '17 at 16:38
  • @NealWalters exactly, see also my answer with sample. – Styxxy Nov 06 '17 at 16:41
  • I would use configuration file as suggested by Microsoft. https://msdn.microsoft.com/en-us/magazine/mt632279.aspx and here https://stackoverflow.com/questions/45034007/using-app-config-in-net-core Here is simple example https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration?tabs=basicconfiguration – Yuri S Nov 06 '17 at 16:44
  • @Yuri, I saw both of those before posting, and referenced one of them in my post. It just seems odd to me that Visual Studio gives you the environment variables "tooling", which is somewhat similar to AWS-Lambda environment variables; but then there wasn't anything I could see in those articles about how it related to the environment variables. – NealWalters Nov 06 '17 at 16:51
  • https://blogs.msdn.microsoft.com/luisdem/2017/03/19/net-core-1-1-how-to-publish-a-self-contained-application/ Might be useful if you want .exe files, but does not solve your env var problem – M Y Nov 06 '17 at 16:55
  • They are not related and I would use config API but choice is yours – Yuri S Nov 06 '17 at 17:04

1 Answers1

4

Create a cmd file that sets the variable. It will only be set as "environment variable" while the cmd is being executed, afterwards the "environment variable" is not anymore set.

Example dotnet core app:

class Program
{
    static void Main(string[] args)
    {
        string myvar = System.Environment.GetEnvironmentVariable("MyEnvironmentVariable");
        Console.WriteLine(myvar);
    }
}

Example cmd to call (let's assume the compiled dll is called "Foo.dll"):

SET MyEnvironmentVariable=Hello
dotnet Foo.dll

The output should be "Hello".

Personally I would recommend a configuration file (which can be configured with the correct values at deploy time). This is (also) the first place to intuitively look in for configuration. Environment variables are kind of "magic". A "run.cmd" is also something that easily can be overlooked to be used.

Styxxy
  • 7,462
  • 3
  • 40
  • 45
  • Ok, thanks. Will try hopefully today, and this should work for my specific case and answers the specific question I asked. The main consideration then is the env-variables should be named appropriately so as not conflict with other programs (probably prefixed with the ApplicationName) I think the best enterprise route is then to do the a config.json file. – NealWalters Nov 06 '17 at 16:46
  • @NealWalters The value is only set during the run of the command. Afterwards the set value is gone for this run. Personally I try to use configuration files, as this is much clearer and can be set correctly at deploy time (environment variables are something that is easily overlooked). – Styxxy Nov 06 '17 at 16:49
  • 2
    On the flip side, modern container/cloud deployments extensively prefer environment variables. For example: https://12factor.net/config – omajid Nov 06 '17 at 17:43