Skip to main content

Multiple Enviroment Support in Asp.net core.




Introduction.

ASP.NET Core has introduce new feature and many more performance enhancement. ASP.NET Core configures app behavior based on the runtime environment using an environment variable.

We can set any value to ASPNETCORE_ENVIRONMENT but three environment value are in-build supported by framework are Development, Staging, and Production. by default value of ASPNETCORE_ENVIRONMENT is Production.


Description.

Let use see how we can set ASPNETCORE_ENVIRONMENT using Visual studio. the environment variable settings can be specified in our project's debug profiles.


Above image shows current environment is set as Development.

when we are modify any settings within the project will be refect in launchSettings.json, launchSettings.json is located into properties folder. The "launchSettings.json" file holds the setting, which is specific to each profile used to launch the Application.


Asp.net core read environment variable value at app startup and store value into IHostingEnvironment.EnvironmentName. The development environment can enable features that shouldn't be exposed in production. the environment setting may be used to define different strategy for different environment Development or Production. for example minified js can be used in production environment and un-minified js can be used in Production.

we can check environment value in startup file using in-build extension method like IsDevelopment, IsProduction or by IsEnvironment(method). For example, IsDevelopment returns true if the environment name is Development, else false. 

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
  if(env.IsDevelopment())  
  {  
   // ToDO Current environment name is "Development"  
  }  
  else  
  {  
   // ToDO Current environment name is other than "Development"  
  }  
 }

Asp.net core application can only use single environment, as value set against ASPNETCORE_ENVIRONMENT variable.


Environment based Startup class and methods.

Asp.net core support environment based startup configuration.  we can programmatically control how your application behaves to environment in startup stage.

However, if a class exists named Startup{EnvironmentName} for example StartupDevelopment, and the ASPNETCORE_ENVIRONMENT environment variable matches that environment startup class, then that Startup class is used. Thus, you could configure Startup for development same way we can define separate for Production environment StartupProduction.

public class StartupDevelopment
{

    public IConfiguration Configuration { get; }
    public StartupDevelopment(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }


    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
          
        }
    }
}

Once we create environment specific startup classes then next we need to register it into program.cs class as below. we need to read startup file based on environment by using  .UseStartup(Assembly.GetEntryAssembly().FullName)

public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseStartup(Assembly.GetEntryAssembly().FullName)
                .Build();
    }


We can also define Configure() and ConfigureServices() method support based on environment eg. Configure{EnvironmentName}(), which are configured within startup class.

public class Startup
{

    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureDevelopmentServices(IServiceCollection services)
    {
        services.AddMvc();
    }


    public void ConfigureDevelopment(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
          
        }
    }
}

For example if we define both for development environment then it look like ConfigureDevelopment() and ConfigureDevelopmentServices();

Comments

Popular posts from this blog

Custom AuthorizeAttribute in ASP.NET Core.

Introduction. Filter in MVC are attribute which can be apply to controller and action method. filters allow us to write our custom code before and after method execution. filters can be implemented into different level of scope Global, Class, and Method level. MVC provides different types of filters. The Authorization filter will run first and are used to determine whether the current user is authorized or unauthorized for the current request. In this article we will learn how to create custom authorization filter, were you can write your own authorization framework. Description. For creating Authorization filter IAuthorizationFilter interface must be inherited. For example, the following code demonstrate custom Authorization below CustomAuthorization attribute allow only Home controller to access the particular method under home controller class else it will return "error" in response if it is called via ajaxcall, otherwise it will be redirect to Sessi

How to use Log4Net in .Net Core 2.0 Application.

1. Introduction Logging frameworks are important because they make it easy to write your logs to different places by simply changing your configuration. You can write your .NET logs to a file on disk, a database, a log management system 2. Install Nuget Package For nlog  To use Nlog logging you need to first add Nlog pluging for adding pluging you can do it by different way. 1) Manage Nuget Packages. 2) Nuget command. you can find nuget command for Log4Net below. PM> Install-Package log4net -Version 2.0.8 3. Add log4net.config file We need to Add New to your project with name log4net.config Refer below code for log4net.config for logging log into file. Root is neccesary in log4net.config, in which we can define log level and appender-ref in which we can define appender for eg. FileAppender, ConsoleAppender. Layout : In layout we can define custom parameter sh

What are PascalCase and CamelCase In Programing Language?

What are PascalCase and CamelCase In Programing Language?. Pascal Case  :-  In programming,  PascalCase   means that the first letter of every word in the name is capitalized. In Pascal Case if acronym exists in the word then only the first letter of that acronym should be capital. Eg:  ThisWordIsInPascalCase  . No other characters are used to separate the words, like hyphens or underscores. For example: BackColor TimeUtc FirstName CamelCase  :-    camelCase  or  CamelCase ; also known as  camel caps  or more formally as  medial capitals.  It is the same as Pascal Case, but the first letter of the first word is lowercased. this article calls the two alternatives  upper camel case  (initial upper case letter also known as Title Case) and  lower camel case  (initial lower case letter). Some people and organizations, notably  Microsoft  ,  use the term  camel case  only for lower camel case. For example: backColor timeUtc firstName Snake_case  :-PascalCase and camelCase