appsetting.json In Console App - Using Dependency Injection In .NET Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome back to code switch [Music] okay so you might have seen there is a app settings.json file when you create an asp.net core application but in case of a console application that is not there so how can we enable it using dependency injection let's have a look alright so as you can see i have a console application going on here i can show you so we have the demo and i have a program.cs file over there i click over there and he can just see some console.writeline let's try to run this okay just the welcome message over here so in order to add the app settings file we can just add a json file for now so add new item and here we can search for json okay so select the json file and name it app settings dot json that is the conversion that we have followed right click on add okay now we have the app settings store json let's open it and add a key value pair okay so now i'm going to add a key called a name maybe okay and the value as code switch our channel name okay just save it okay now we have added the key value to our app settings.json okay so as you can see here and what we need to do the next is to make it copy to output why we are doing this is to make sure that every time it changes we need to add it to the bin folder okay now make sure you save it everything now here we are going to start our dependency injection part okay so the first step is always to create a service container so you can say that create a service collection for the dependency injection and here we can define a new variable or service collection or something service collection equal to new service question but where we get this class service collection so for that you need to install some nugets right so go to your project and go to your nuget package manager and here on the browse tab you can search for dependency injection and there we go so here we can see microsoft extension difference injection just install it it might take some time because it's a big package okay let's see whether it completes okay looks like it done okay now we need another few packages so you can search for configuration because we are dealing with the configuration we need three package from here so the first one is actually microsoft.extension.configuration you can install that okay so the next one is microsoft.extension.configuration.file extensions install that and the third one is just below it microsoft extension.configuration.json why because we are using the json file so we have installed all the three required uh configuration and the depends injection okay so using the dependency injection we can resolve this one service collection error that we are getting okay click on that blue that bulb and resolve this all right so we have created a service question so the next thing is we need to build a configuration we need to tell the configuration classes that this is our file that you need to make okay so for that as we always use in dotnet code we can create an eye configuration for this an interface i configured let's initialize and remove the l [Music] and resolve it and configuration is coming from the extensions dot configuration okay now we have the i configuration now we need to build a configuration okay so we can say that configuration equals okay new conflict no no not this one configuration builder okay so visual studio is trying to suggest me okay the first thing that we need to add is to add a set base path that we can get from the app context but there is a cache because we need to get the parent full name of that so what we can do is app context dot base directly let's let's cut this we need to put it inside the director we need to get the parent entry so let's call the let's call the directory it is coming from system.io directory resolve it system.here and you can call the getparent because that will give the parent name of that okay put the app contest to a base editor here and get the full name all right now we need to tell it to add our json file so let's do it add json file and name our json file so in this case we have app settings.json and it shows the red line because we are not building it so let's build it dot build okay now as you can see we have a service collection and we have a configuration okay so let's remove this unwanted windows okay now we need to add this configuration to the service collection okay like as you can see add the configurations to the service collection okay so we have defined the configuration over here and we have service question over here yeah so let's call the service collection and let's add it as a okay so i had single turn [Music] and okay so we have the typos i configuration so we can add it as the configuration okay now this is all it takes now we have done everything now for testing we can do one thing we can create a test class maybe so add a class new class okay and we can name the uh i think test will be fine right so let's name it the classes test note this class okay and click on add okay now we have created a blank class now let's add a constructor ctor for the short okay now in the constructor we can receive an i configuration from the dependence injection so define the i configuration and resolve it extensions.configurations and let's name it and create a local variable for that we can create a local read-only variable okay so i have created a local configuration and assign it during the initialization okay now let's create a demo function to check whether everything is working so i'm saying that test method maybe okay so this method what this method need to do you need to read the data from the json and it need to print it out so let's create a variable to read the data from json so data from json maybe okay just file equals so okay we have the configurations we get from the different suggestion from that get the section name and get the value and if you wonder where this name come from as we see if you open the con app settings.json you can see that we have the key as name and the address code switch right okay now we get the data from the json file it picks that value for the name do this now let's write it into the console so that happens json file and the semicolon yeah all right so when we called this this is what happened now there is a catch we need to add this class to the different suggestion also so what i'm going to do here i'm trying to add this class okay so it's a concrete class it didn't have an interface i have i have not created an interface this is for demo right so that's why so let's add the test class okay that's it it's a singleton okay now we have added both of the items right now in order to we need if we need to get an instance of what we added to the service collection we can define a service provider okay so i'm defining a service provider that is service collection dot build service yes automatically suggested it for me and from that service provider i need to get an instance of the test right that also vs also provided me so this is actually an instance of a test so let's name it as test instance maybe for the demo as so we can understand it okay so we get an instance of the test now from that instance i am calling the test method that we created okay so i'm putting the test method now inside this test method what we can do we can put a debugger so that we can see how the execution flows okay now let's study all right so i think the test method is called the test method is called okay now the debugger is here and you can see we are trying to get the data so this i configuration everything is injected so if you look at the configuration see it's not none it is injected okay now we are getting the data from the json let's see if we are properly able to get that i hope yeah look at coaches now remove the debugger and complete the project okay now let's try to run it again [Music] there we go but we put in the json is here now let's try to change it and try to see it okay so what we want to do is code switch youtube channel the reason why i always try to get the latest is because we added us if we change you are copying it to the pin directory look yep code switch youtube channel okay that's all about today [Music] you
Info
Channel: Code Switch
Views: 1,293
Rating: undefined out of 5
Keywords: c#, appsettings.json, .net core
Id: tQdNlju2UXo
Channel Id: undefined
Length: 10min 15sec (615 seconds)
Published: Sun Jul 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.