CheapASPNETHostingReview.com | Best and cheap ASP.NET Core hosting. This short post about how to set the hosting environment in ASP.NET Core.
However, if this is a capability you think you will need, you can use a similar approach to the one I use in that post to set the environment using command line arguments.
This approach involves building a new IConfiguration
object, and passing that in to the WebHostBuilder
on application startup. This lets you load configuration from any source, just as you would in your normal startup method, and pass that configuration to the WebHostBuilder
using UseConfiguration
. The WebHostBuilder
will look for a key named "Environment"
in this configuration, and use that as the environment.
For example, if you use the following configuration.
1 2 3 4 5 6 7 8 9 10 11 | var config = new ConfigurationBuilder() .AddCommandLine(args) .Build(); var host = new WebHostBuilder() .UseConfiguration(config) .UseContentRoot(Directory.GetCurrentDirectory()) .UseKestrel() .UseIISIntegration() .UseStartup<Startup>() .Build(); |
You can pass any setting value with this setup, including the “environment variable”:
1 2 3 4 5 6 7 8 | > dotnet run --environment "MyCustomEnv" Project TestApp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation. Hosting environment: MyCustomEnv Content root path: C:\Projects\Repos\MyCoolProj\src\MyCoolProj Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down. |
This is fine if you can use command line arguments like this, but what if you want to use environment variables? Again, the problem is that they’re shared between all apps on a machine.
However, you can use a similar approach, coupled with the UseEnvironment
extension method, to set a different environment for each machine. This will override the ASPNETCORE_ENVIRONMENT
value, if it exists, with the value you provide for this application alone. No other applications on the machine will be affected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Program { public static void Main(string[] args) { const string EnvironmentKey = "MYCOOLPROJECT_ENVIRONMENT"; var config = new ConfigurationBuilder() .AddEnvironmentVariables() .Build(); var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseEnvironment(config[EnvironmentKey]) .UseIISIntegration() .UseStartup<Startup>() .UseApplicationInsights() .Build(); host.Run(); } } |
To test this out, I added the MYCOOLPROJECT_ENVIRONMENT
key with a value of Staging
to the launch.json file VS uses when running the app:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <span class="token punctuation">{</span> <span class="token property">"profiles"</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"EnvironmentTest"</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"commandName"</span><span class="token operator">:</span> <span class="token string">"Project"</span><span class="token punctuation">,</span> <span class="token property">"launchBrowser"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token property">"environmentVariables"</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"ASPNETCORE_ENVIRONMENT"</span><span class="token operator">:</span> <span class="token string">"Development"</span><span class="token punctuation">,</span> <span class="token property">"MYCOOLPROJECT_ENVIRONMENT"</span><span class="token operator">:</span> <span class="token string">"Staging"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token property">"applicationUrl"</span><span class="token operator">:</span> <span class="token string">"http://localhost:56172"</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Running the app using F5, shows that we have correctly picked up the Staging
value using our custom environment variable:
1 2 3 4 | Hosting environment: Staging Content root path: C:\Users\Sock\Repos\MyCoolProj\src\MyCoolProj Now listening on: http://localhost:56172 Application started. Press Ctrl+C to shut down. |
With this approach you can effectively have a per-app environment variable that you can use to configure the environment for an app individually.
Summary
On shared hosting, you may be in a situation when you want to use a different IHostingEnvironment
for multiple apps on the same machine. You can achieve this with the approach outlined in this post, building an IConfiguration
object and passing a key to WebHostBuilder.UseEnvironment
extension method.