New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separator character for environment variables #469

Closed
JauernigIT opened this Issue Jun 23, 2016 · 10 comments

Comments

Projects
None yet
6 participants
@JauernigIT

JauernigIT commented Jun 23, 2016

I have ASP.NET Core 1.0 RC2 installed on my Windows machine and just came over the requirement to override a configuration entry in appsettings.json (section "AppSettings") with the value of an environment variable (Windows Batch script).

I just thought it would be possible with SET AppSettings_MyKey=value from command line, because I thought RC2 changed the separator character from : to _. But it didn't work... what worked was SET AppSettings:MyKey=value -- with this the value is perfectly overridden.

Is my assumption wrong that separator character changed to _ or is this a bug? Will there be any changes in RTM?

@moozzyk

This comment has been minimized.

Show comment
Hide comment
@JauernigIT

This comment has been minimized.

Show comment
Hide comment
@JauernigIT

JauernigIT Jun 24, 2016

Unfortunately, it doesn't work... as I understand the code, the __ gets replaced with :, but I don't see that the appsetting gets overridden with the environment variable...

I have the following section in appsettings.json:

"AppSettings": {
    "MyKey": "TO_OVERRIDE"
}

And just before starting dotnet run, I want to override it from command line / batch file:

SET AppSettings__MyKey=value

This doesn't work, whereas SET AppSettings:MyKey=value works. But a colon in the variable name is awkward to handle in Windows command line.

JauernigIT commented Jun 24, 2016

Unfortunately, it doesn't work... as I understand the code, the __ gets replaced with :, but I don't see that the appsetting gets overridden with the environment variable...

I have the following section in appsettings.json:

"AppSettings": {
    "MyKey": "TO_OVERRIDE"
}

And just before starting dotnet run, I want to override it from command line / batch file:

SET AppSettings__MyKey=value

This doesn't work, whereas SET AppSettings:MyKey=value works. But a colon in the variable name is awkward to handle in Windows command line.

@cesarbs

This comment has been minimized.

Show comment
Hide comment
@cesarbs

cesarbs Jun 30, 2016

Contributor

This is by design. On Windows you should use :; on non-Windows, use __ (double underscore).

cc @HaoK

Contributor

cesarbs commented Jun 30, 2016

This is by design. On Windows you should use :; on non-Windows, use __ (double underscore).

cc @HaoK

@JauernigIT

This comment has been minimized.

Show comment
Hide comment
@JauernigIT

JauernigIT Jul 1, 2016

Yeah, and then you have great fun in a batch script where you want to access the content of the AppSettings:MyKey variable. I cannot imagine why this could be by (good) design and why there are differences for different platforms here. Shouldn't it be consistent? But well, since I know the answer now, I'll close the issue.

JauernigIT commented Jul 1, 2016

Yeah, and then you have great fun in a batch script where you want to access the content of the AppSettings:MyKey variable. I cannot imagine why this could be by (good) design and why there are differences for different platforms here. Shouldn't it be consistent? But well, since I know the answer now, I'll close the issue.

@JauernigIT JauernigIT closed this Jul 1, 2016

@joelharkes

This comment has been minimized.

Show comment
Hide comment
@joelharkes

joelharkes Feb 21, 2017

To be honest i think this is terrible by design.

Especiall when trying t move AspNet Core forward to OS platform independent software and docker containers.

Kubernetes uses undescores as seperators. Docker engine uses underscore as separators, node uses unserscore as seperators. (and capital letters)

Dotnet? uses : and as alternative a double undescore?

Seriously? and then in code there is:

    //public class EnvironmentVariablesConfigurationProvider : ConfigurationProvider
    private const string MySqlServerPrefix = "MYSQLCONNSTR_";
    private const string SqlAzureServerPrefix = "SQLAZURECONNSTR_";
    private const string SqlServerPrefix = "SQLCONNSTR_";
    private const string CustomPrefix = "CUSTOMCONNSTR_";

I'd vote for moving this all to UPERCASE_UNDERSCORE for environment variables. You would only shoot yourself in the leg if you do not follow this uniform design used anywhere else. (you wont reach those linux/opensource developers)

joelharkes commented Feb 21, 2017

To be honest i think this is terrible by design.

Especiall when trying t move AspNet Core forward to OS platform independent software and docker containers.

Kubernetes uses undescores as seperators. Docker engine uses underscore as separators, node uses unserscore as seperators. (and capital letters)

Dotnet? uses : and as alternative a double undescore?

Seriously? and then in code there is:

    //public class EnvironmentVariablesConfigurationProvider : ConfigurationProvider
    private const string MySqlServerPrefix = "MYSQLCONNSTR_";
    private const string SqlAzureServerPrefix = "SQLAZURECONNSTR_";
    private const string SqlServerPrefix = "SQLCONNSTR_";
    private const string CustomPrefix = "CUSTOMCONNSTR_";

I'd vote for moving this all to UPERCASE_UNDERSCORE for environment variables. You would only shoot yourself in the leg if you do not follow this uniform design used anywhere else. (you wont reach those linux/opensource developers)

@joelharkes

This comment has been minimized.

Show comment
Hide comment
@joelharkes

joelharkes Feb 21, 2017

For those who like it clean and simple: (Note it is now case-insensitive, on default)

public class OsIndependentEnvironmentVariablesConfiguration : ConfigurationProvider, IConfigurationSource
{
    public StringComparer StringComparer { get; set; }

    public OsIndependentEnvironmentVariablesConfiguration()
    {
        StringComparer = StringComparer.OrdinalIgnoreCase;
    }
    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return this;
    }

    public override void Load()
    {
        Data = new Dictionary<string, string>(StringComparer);

        var envs = Environment.GetEnvironmentVariables().Cast<DictionaryEntry>();
        foreach (var env in envs)
        {
            var key = ((string) env.Key).Replace("_", ConfigurationPath.KeyDelimiter);
            Data[key] = (string)env.Value;
        }
    }
}

joelharkes commented Feb 21, 2017

For those who like it clean and simple: (Note it is now case-insensitive, on default)

public class OsIndependentEnvironmentVariablesConfiguration : ConfigurationProvider, IConfigurationSource
{
    public StringComparer StringComparer { get; set; }

    public OsIndependentEnvironmentVariablesConfiguration()
    {
        StringComparer = StringComparer.OrdinalIgnoreCase;
    }
    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return this;
    }

    public override void Load()
    {
        Data = new Dictionary<string, string>(StringComparer);

        var envs = Environment.GetEnvironmentVariables().Cast<DictionaryEntry>();
        foreach (var env in envs)
        {
            var key = ((string) env.Key).Replace("_", ConfigurationPath.KeyDelimiter);
            Data[key] = (string)env.Value;
        }
    }
}
@chrispaynter

This comment has been minimized.

Show comment
Hide comment
@chrispaynter

chrispaynter Aug 27, 2017

@joelharkes this looks like just the ticket to get around this. Do you have a link to this code fully implemented anywhere? Thanks.

chrispaynter commented Aug 27, 2017

@joelharkes this looks like just the ticket to get around this. Do you have a link to this code fully implemented anywhere? Thanks.

@joelharkes

This comment has been minimized.

Show comment
Hide comment
@joelharkes

joelharkes Aug 27, 2017

@chrispaynter https://github.com/joelharkes/AspNetCore.Better.ConfigurationSources

I lamely prefixed it better. I've been off the dotnet core cloud project in m company for a while now so I'm not sure how up to date it is, but probably fine.

There is also one for json config which is really nice (node_config like)

joelharkes commented Aug 27, 2017

@chrispaynter https://github.com/joelharkes/AspNetCore.Better.ConfigurationSources

I lamely prefixed it better. I've been off the dotnet core cloud project in m company for a while now so I'm not sure how up to date it is, but probably fine.

There is also one for json config which is really nice (node_config like)

@chrispaynter

This comment has been minimized.

Show comment
Hide comment
@chrispaynter

chrispaynter commented Aug 27, 2017

Nice one, thanks @joelharkes

@onionhammer

This comment has been minimized.

Show comment
Hide comment
@onionhammer

onionhammer Jul 6, 2018

It would be nice if AddEnvironmentVariables had an overload for 'seperator'. My app has no need for double-underscores; a single underscore would be preferable

onionhammer commented Jul 6, 2018

It would be nice if AddEnvironmentVariables had an overload for 'seperator'. My app has no need for double-underscores; a single underscore would be preferable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment