Taking advantage of configuration in .net core 2.2+ console applications

Taking advantage of configuration in .

net core 2.

2+ console applicationsLarry SchoenemanBlockedUnblockFollowFollowingApr 4This is related my prior article on dependency injection in .

net core 2.

2 console apps.

I am assuming you’ve read that before coming here.

Otherwise this will make no sense.

We’ve established a basic console application which can now use dependency injection using the .

net core framework (2.

2 or later).

From here we need to add a couple of necessary elements.

ConfigurationLoggingDatabase context configuration for Entity Framework CoreIn this article, we will focus on setting up configuration.

Logging and database connectivity configuration will follow.

First things first.

Start with the code from the above referenced article.

If you don’t still have that around, well…sorry!Now, go to your handy dandy NuGet Package manager and add the following packages:Microsoft.

Extensions.

ConfigurationMicrosoft.

Extensions.

Configuration.

JsonIf you don’t know how to do this, I have no idea why you are bothering to read this article, as you probably don’t have adequate knowledge of any of this.

Oh well.

Not my circus, not my monkeys!We are only going to cover a few situations here.

You can use one of the providers for environment variables etc as you need them.

Adding configuration, in the context of our dependency injection enabled console application is straightforward.

Open Program.

cs and go to the method we previously created, ConfigureServices:private static IServiceCollection ConfigureServices(){ IServiceCollection services = new ServiceCollection(); services.

AddTransient<ITestService, TestService>(); // IMPORTANT!.Register our application entry point services.

AddTransient<ConsoleApplication>(); return services;}We are going to add a call to set up the object we need for configuration, and then add that object to our DI container so we can get to it.

Since there is no need for more than one configuration object per application, we are going to create a Singleton.

I know some people are not fans, but since this is an injected singleton, you can test it!.Go you!So our new ConfigureServices method looks like:private static IServiceCollection ConfigureServices(){ IServiceCollection services = new ServiceCollection(); // Set up the objects we need to get to configuration settings var config = LoadConfiguration(); // Add the config to our DI container for later user services.

AddSingleton(config); services.

AddTransient<ITestService, TestService>(); // IMPORTANT!.Register our application entry point services.

AddTransient<ConsoleApplication>(); return services;}We need to define our LoadConfiguration method now.

It just needs to set us up to read from an application config file:public static IConfiguration LoadConfiguration(){ var builder = new ConfigurationBuilder() .

SetBasePath(Directory.

GetCurrentDirectory()) .

AddJsonFile("appsettings.

json", optional: true, reloadOnChange: true); return builder.

Build();}This assumes our application uses a file named appsettings.

json to hold our config settings (I REALLY hope that’s obvious to you.

Again, if not, why are you reading this?)If you need them you can call AddEnvironmentVariables, AddXmlFile and so on, as long as you grab the appropriate NuGet package.

In our code, we set up a builder, give it what we want it to be able to access (paths, files, environment variables, etc) and then call build to give us back a configuration.

We then pass this back, and drop it into our DI container.

services.

AddSingleton(config);Essentially, we are done.

Now, all we need to do is inject and go!Let’s say we have an appsettings.

json file that looks like:{ "Location": "Home", "ConnectionStrings": { "Storage": "Sample" }}In our console application’s ConsoleApplication class, we can add the configuration object to our constructor:public class ConsoleApplication{ private readonly IConfiguration _configuration; public ConsoleApplication(IConfiguration configuration) { _configuration = configuration; } .

Now in our methods, we can use our injected configuration object:public void DoSomething(){ var location = _configuration.

GetValue<string>("Location");.

There you go!.More of the .

net core’s framework usable by the lowly console application.

Go play!.More to come about logging and database connectivity.

.

. More details

Leave a Reply