Sdk: Can support environment variables first in the feature?

Created on 6 Jul 2018  ·  20Comments  ·  Source: dotnet/sdk

In use docker scenes,we must write the code support environment variables first,
hope support environment variables first in the feature

Most helpful comment

@benaadams @andymac4182 Thank you very much, It's worked !
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); }

All 20 comments

@lc273734858 I don't understand the request. Can you provide more details? Which feature would you like to support environment variables?

when you run your app in docker or kubernetes, start your app with "docker run -e configarg value",in java you need do nothing,but in dotnetcore you wirte it by yourself.

I'm not very familiar with docker, so I'm still having trouble understanding the request.

Paging @richlander who would have more context.

I'm so sorry to that my english so poor.
When use docker ,you make one image must run in all environment,example dev/test/prd, you must set "docker run -e connectionstring xxxxx"....

No problem at all! I've reached out to some people who work more regularly with Docker to see if they can help here.

@nguerrera Thank you very much!

Now I need close my issue?

No, the issue can stay open while we wait for some other people to respond.

OK

I believe you are talking about environment promotion. What specific way do you want to control the product in Docker with environment variables that isn’t possible today?

I can write code like this....
`
public string AppId
{
get { return appid; }
set
{
appid = Environment.GetEnvironmentVariable(nameof(AppId));
if (string.IsNullOrEmpty(appid)) appid = value;

        }
    }
    public string AppSystem
    {
        get { return appsystem; }
        set
        {
            appsystem = Environment.GetEnvironmentVariable(nameof(AppSystem));
            if (string.IsNullOrEmpty(appsystem)) appsystem = value;

        }
    }`

but it's cumbersome,hope simpler

Are you using ASP.NET Core?

If so, it looks like https://docs.microsoft.com/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1&tabs=basicconfiguration can make it easy to bind environment variables to a class.

Thanks, I saw the url,
It's depend appsettings.EnvironmentName.json ,then must Pre-written appsetting.EnvironmentName.json ,that's not what I want, I hope when I run it in any where, I can set the "appsetting.json"'s setting, but
do not change my docker Image.
Just use the "docker run -e connectionstring xxxxx" to set the config value.

This article covers the configuration system and how overriding works. http://www.paraesthesia.com/archive/2018/06/20/microsoft-extensions-configuration-deep-dive/ If you search for Environment variables: there is some examples of how setting variables via environment variables works.

@andymac4182 Thanks for your help ,I will try to use the article.

If its an aspnetcore app using the default builder (e.g. WebHost.CreateDefaultBuilder(args)) then you would just need to prefix you environment variable with ASPNETCORE_ e.g.

docker run -e ASPNETCORE_connectionstring xxxxx

This is so general environment variables don't accidentally overwrite your configuration.

You can also use it without the prefix by adding .AddEnvironmentVariables() e.g.

WebHost.CreateDefaultBuilder(args)
  .AddEnvironmentVariables()

Then use docker run -e connectionstring xxxxx; but that risks accidentally overwriting some settings with environment variables that are set by something else (as there is no-prefix to filter them)

@lc273734858 How did you go?

@andymac4182 I'm so sorry, I'm busy in my project now ,have no enough time to try with the article.
I will answer to u when I try it finished.

@benaadams @andymac4182 Thank you very much, It's worked !
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

srivatsn picture srivatsn  ·  3Comments

moozzyk picture moozzyk  ·  3Comments

gkhanna79 picture gkhanna79  ·  3Comments

thomaslevesque picture thomaslevesque  ·  3Comments

fmorriso picture fmorriso  ·  3Comments