klabautermann123
8/8/2017 - 7:09 PM

ASPNET_CORE Environment Config  export ASPNETCORE_ENVIRONMENT=DEV && dotnet run

ASPNET_CORE Environment Config  export ASPNETCORE_ENVIRONMENT=DEV && dotnet run

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

namespace EDDA
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                // You can set the Envrironment on the Mac or Linux like this
                //   export ASPNETCORE_ENVIRONMENT=DEV && dotnet run
                // In windows you need to set the global variable ASPNETCORE_ENVIRONMENT by using the SET command
                // -------------------------------------------------------------------------------------------------
                // The environment kann be also setted manually
                // -------------------------------------------------------------------------------------------------
                .UseEnvironment("Production")
                .Build();

            host.Run();
        }
    }
}

First steps

  1. First locate the file appSettings.json and defined all the constants, Urls and values that your application will use.

  2. Then create two copy of appSettings.json and name them as following:

appSettings.DEV.json ==> override all values accordindly to match the DEV environment
appSettings.QS.json ==> override all values accordindly to match the QS environment

Remark: appSettings.json will be the PROD

  1. Verify this code snippet on the Startup.cs (constructor) file and make sure that .AddEnvironmentVariables() is present:

    var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); --> Enable ConfigurationVariables

  1. Create the Configuration model to the used by the configuration service e.g. Config.cs

     public class Config
     {
     
       public string environment { get; set; }
       public string DOC_ID { get; set; }
       public string oracleHost { get; set; }
       public string oracleServiceName { get; set; }
       public string oraclePort { get; set; }
       public string oracleDbUser { get; set; }
       public string oracleDbPassword { get; set; }
       public string oracleConnectionString { get; set; }
       public string strs_POST_URL { get; set; }
     }
    
  2. Add this line of code to the ConfigureServices Method:

     // options: analog zu System.ConfigurationManager
     services.AddOptions();
     services.Configure<Config>(Configuration.GetSection("Config"));  
    
  3. The appSetting.json should look like this:

    { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "Config" : { "environment" :"PROD", "DOC_ID" :"DMP_002", "oracleHost" :"RPAR", "oracleServiceName" :"RPAR", "oraclePort" :"1584", "oracleDbUser" :"edmp_admin", "oracleDbPassword" :"sommer", "oracleConnectionString" :"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={2})));User Id={3};Password={4};", "strs_POST_URL": "http://strsp2v0:8292/online" }

    }

Setting the global Variable ASPNETCORE_ENVIRONMENT:

  1. You can set the Envrironment on the Mac or Linux like this export ASPNETCORE_ENVIRONMENT=DEV && dotnet run

  2. In windows you need to set the global variable ASPNETCORE_ENVIRONMENT by using the SET command SET ASPNETCORE_ENVIRONMENT=DEV

  3. or in the Code:

     var host = new WebHostBuilder()
         .UseKestrel()
         .UseContentRoot(Directory.GetCurrentDirectory())
         .UseIISIntegration()
         .UseStartup<Startup>()
         .UseEnvironment("Production")   <--
         .Build();
     host.Run();