Giter Site home page Giter Site logo

console-json-configuration's Introduction


Console Json Configuration

Disclaimer: This repo is for personal use only. I pretty much ripped this project off some blog. But the blog post forgot to mention one step which crashed the application. So I added the step here for next time.

Reference Blogs

1. Install The Required Nuget Packages

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Binder
  • Microsoft.Extensions.Configuration.EnvironmentVariables
  • Microsoft.Extensions.Configuration.Json

2. Configure Program.cs

static void Main(string[] args)
      {
          var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
          var builder = new ConfigurationBuilder()
              .AddJsonFile($"appsettings.json", true, true)
              .AddJsonFile($"appsettings.{environment}.json", true, true)
              .AddEnvironmentVariables();

          var configurationRoot = builder.Build();
          var appConfig = configurationRoot.GetSection(nameof(AppConfig)).Get<AppConfig>();

          Console.WriteLine(appConfig.Environment);
      }

3. Configure appsettings.json, appsettings.Development.json and appsettings.Production.json

  1. appsettings.json {"AppConfig": { "Environment": "Default" } }

  2. appsettings.Development.json {"AppConfig": { "Environment": "DEV" } }

  3. appsettings.Production.json {"AppConfig": { "Environment": "PROD" } }

4. Configure launchSettings.json

{
  "profiles": {
    "ConsoleJsonConfigurationExample (Dev)": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ConsoleJsonConfigurationExample (Prod)": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  }
}

5. Configure .csproj File

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="appsettings.Development.json" />
    <None Remove="appsettings.json" />
    <None Remove="appsettings.Production.json" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="appsettings.Production.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="appsettings.Development.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
  </ItemGroup>

</Project>

We add this configuration becasue we need to make sure that the JSON files are coppied over to the output directory of our application.

6. Run Application

console-json-configuration's People

Contributors

stevenbuglione avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.