Monday 5 December 2016

Reading appsettings.json in .Net Core Console Application under Linux

Recently I was looking at how to load and use appsettings.json file in .Net Core Console application. Many resources on .Net forums and sites were descripting use of appsettings.json file in ASP.NET Core web App, but very little described appsettings with .Net Core Console app. I managed to created sample .Net Core app with the minimum required libraries. Creation of this sample program I am going to describe here. Let's call our App "appsettings".
$mkdir appsettings
$cd appsettings
$dotnet new
$dotnet restore
Now create appsettings.json file:
{
    "AppSettings": {
        "Date": "1900-01-01"
    }
}
Date is the setting I am interested in loading from AppSettings section.

Edit project.json file to include "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0" in dependencies section. This is the only extra library we need in order to use ConfigurationBuilder functions.
{
    "version": "1.0.0-*",
    "buildOptions": {
        "debugType": "portable",
        "emitEntryPoint": true,
        "outputName": "AppSettings"
    },
    "dependencies": {},
    "frameworks": {
        "netcoreapp1.0": {
            "dependencies": {
                "Microsoft.NETCore.App": {
                    "type": "platform",
                    "version": "1.1.0"
                },
                "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0"
            },
            "imports": "dnxcore50"
        }
    }
}
Now lets edit the Program.cs file. We are going to load the Date setting and display it in Console. Don't forget to include "using Microsoft.Extensions.Configuration;"
using System;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace ConsoleApplication
{
    public class Program
    {
        public static string AppSettingsFile = "appsettings.json";
        public static string AppSettingsSection = "AppSettings";
        public static string DateSetting = "Date";

        public static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(Program.AppSettingsFile, optional: true, 
                             reloadOnChange: true);
            
            var config = builder.Build();
            var appSettings = config.GetSection(Program.AppSettingsSection);
            string dateString = appSettings[Program.DateSetting];
            Console.WriteLine("Date:" + dateString);
        }
    }
}
That is pretty much all. Now let's build and run it.
$dotnet build
$dotnet run
Date:1900-01-01

3 comments:

  1. Nice article I was really impressed by seeing this article, it was very interesting and it is very useful for Learners. I get a lot of great information from this blog. Thank you for your sharing this informative blog
    Selenium Training in Chennai
    Selenium Course in Chennai

    ReplyDelete
  2. Hai Author Good Information that i found here,do not stop sharing and Please keep updating us..... Thanks.
    hire asp.net developer
    .net development services

    ReplyDelete
  3. It is very good and useful for students and developer .Learned a lot of new things from your post!Good creation ,thanks for good info .Net Online Training Hyderabad

    ReplyDelete