问题
I am working on learning how to properly handle environment variables and configuring my app in multiple environments. I've chosen to read from a config.json in Development, and use environment variables in Production.
I have the following Startup.cs to demonstrate this:
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Configuration;
namespace Variables
{
public class Startup
{
private IConfiguration mConfiguration;
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder();
if (env.IsDevelopment())
{
// Only load from config when in development.
builder.AddJsonFile("config.json");
}
builder.AddEnvironmentVariables();
mConfiguration = builder.Build();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseIISPlatformHandler();
app.UseDeveloperExceptionPage();
app.Run(async (context) =>
{
// Succeeds with hosting:environment=Development, fails with hosting:environment=Production
// ArgumentNullException: Value cannot be null. Parameter name: text
// Environment variable setup in Windows with:
// set bar=1
await context.Response.WriteAsync(mConfiguration["bar"]);
});
}
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
}
My config.json is simply:
{
"bar": 1
}
This succeeds when running as hosting:environment=Development
. This fails though, when running as hosting:environment=Production
. I set up an environment variable in Windows with set bar=1
.
I've also tried utilizing the system environment variables (because I'm not sure if opening up a command prompt and typing set bar=1
does a user environment variable, or system variable), but it fails with the same error when running my app as well.
回答1:
Works on My Machine
With config.json
having { "bar": 1 }
and using this code...
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder();
if (env.IsDevelopment())
{
builder.AddJsonFile("config.json");
}
builder.AddEnvironmentVariables();
mConfiguration = builder.Build();
}
public IConfigurationRoot mConfiguration { get; set; }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync(env.EnvironmentName);
await context.Response.WriteAsync($"\r\n");
await context.Response.WriteAsync(mConfiguration["bar"]);
await context.Response.WriteAsync($"\r\n");
});
}
public static void Main(string[] args) =>
Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
}
...running the following commands...
CMD> dnvm use 1.0.0-rc1-update2
CMD> set bar=3
CMD> dnx web
...displays this in the web browser.
Troubleshooting
Visual Studio
If you're using Visual Studio, restart it after you have changed an environmental variable. Alternatively, define the environmental variable through Visual Studio, so that you do not have to restart.
- Right Click Project > Properties
- Debug
- Environmental Variables
- Add
- Save All
Shell
Restart your shell. dnx web
only picks up environmental variables that are available in its shell. If you opened the shell after defining the environmental variable elsewhere, then you will need to restart your shell.
Check that your shell (PowerShell, command prompt, or bash) is aware of the environmental variable:
PS> $env:bar
CMD> SET bar
$ printenv bar
If your shell is not aware of the environmental variable, set it like this:
PS> $env:bar = 3
CMD> SET bar=3
$ export bar=3
Both
Dump all the environmental variables about which your app is aware:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Run(async (context) =>
{
foreach (var envVar in mConfiguration.GetChildren())
{
await context.Response.WriteAsync($"{envVar.Key}: {envVar.Value}");
await context.Response.WriteAsync($"\r\n");
}
});
}
Check that your app is running in production:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync(env.EnvironmentName);
await context.Response.WriteAsync($"\r\n");
});
}
来源:https://stackoverflow.com/questions/36994077/accessing-environment-variables-from-asp-net-core-1-rc1-iconfiguration