Ehsan Ghanbari

Experience, DotNet, Solutions

Configuration for JSON file in console application of .Net Core

In a console application in .Net Core, you don’t have the appsettings.json file. In order to add this file, add a JSON file with the same name (or any name that you want) and make the following changes to the file in property:

 

 

Now, you should add some configuration to read this file as it’s a console application and it’s not configured by default:

 

class Program

    {

        static void Main(string[] args)

        {

            RegisterServices(args);

        }



        private static ServiceProvider RegisterServices(string[] args)

        {

            IConfiguration configuration = SetupConfiguration(args);

            var serviceProvider = new ServiceCollection();

            serviceProvider.AddSingleton(configuration);

            return serviceProvider.BuildServiceProvider();

        }



        private static IConfiguration SetupConfiguration(string[] args)

        {

            return new ConfigurationBuilder()

                .SetBasePath(Directory.GetCurrentDirectory())

                .AddJsonFile("appsettings.json")

                .Build();

        }

    }

 

Note that the SetBasePath() method lives in Microsoft.Extensions.Configuration.Json library.

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to query over Icollection<> of a type with linq
How to use PagedList In asp.net MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
Using Generic type for type casting in F#
comments powered by Disqus