Reading appsettings.json in .Net Core Class Library Using Dependency Injection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome my channel asp.net core and today I am going to show you how to uh read app settings.json in dot net core class Library so this is the simple demonstrations I will give you and uh in this video tutorial I am giving you two method for accessing app settings.json file using class Library project using uh dependency injection Singleton and uh and also the second method is used to I options so let's watch so create a new uh MVC application first of all and in this application just open app settings.json file and create a new uh you can say that uh array types string and also add a new class Library project so first of all first project is actually at access uh your app settings Association file using your dependency injection so here you can see that reading app settings.json in.net core class Library using dependency injection so uh first of all create a class Library project and MVC application into your application and the next task is to do create a some string in app setting.json file like connection string app settings email ID and account key okay so the next task after creating your uh some string in your or array in app settings.json file just add a new interface the name of your interface you can choose yourself so add this interface in your class Library project Okay so you can use uh in the class Library project Microsoft dot extension dot configuration nuget package so uh to read a key value based configuration you need to install so Microsoft extension.configuration you get package as the package has dependency or Microsoft extension.configuration of sections so install you can nuget packages by using command line tools or you can just Show snippets by the Microsoft as usual okay so here we have in app settings uh interface you can see here we have two uh string values that is email ID and account key and that value is matched with your app settings because you want to access both uh email ID and account key from your app settings dot session file so in the interface that is app settings I app settings just add a tostring okay now after that you have to implement that interface by using the class so you can access first of all change the access to specifier that is public from uh internal also Implement that class from if settings and that is here we have two properties that is email ID and account key and you can use iconfiguration as usual you know I configuration is actually existing in Microsoft extension dot configuration uh package so here we have eye configuration underscore configuration okay you can see that so also as a Constructor dependency you can add configuration I configuration and after that uh just return your email ID by using underscore configuration of square bracket in which you can add first of all the root value of your email ID that is app settings and by using column you can access email ID as well as the account key so first of all just I want to return the email ID so here you can say that email ID okay so by using underscore configuration app settings call an email ID you you want to access it and we just written that a thing by using the email ID that is public property so the similar you can just return account key okay so after that as a dependency injection you can just inject that interface into your program.cs file so just you can add Azure dependency injection so that interface will be injected in program.cs file into your web application so Builder dot service dot add Singleton and in which you can add I app settings and that is your interface name and the implemented class also use so I app settings and implemented class is app settings repo so so you can use this dependency injection and inject it into your controller class as a as a Constructor dependency injection so just open controller class and inject that I app settings as a as a Constructor dependency injection so first of all Define that interface in the top of of your class like I app settings and the name is app settings repo or you can say app app config okay so underscore app config and resolve this issue of your namespace and also inject it and assign it into your Constructor so add parameters in the Constructor so I app settings app config and in the index method you just written your email ID by using access app config dot email ID okay and similarly you can just return your account key so this is the one method you can Implement you can you can Define interface and inject that interface as a dependence injection into your program.cs file and after that you can use that interface as your dependency injection into your home controller class so this is the single method you can access your app settings.json file into your class Library project and in the class Library by using the class Library project you have to access all these properties okay so this is the single method to access email ID and account key from your app settings.json file from your class Library project and the second method you can do directly by using i options so inject uh just just you want to access uh just you can say that in just if you want to access both properties email ID and account key into your class Library project so first of all create a new class in your class Library project that is my class the name of the class is my class and add the similar property name and the type that is similar like integer like string so string is the key value so you can add a class first of all my class and add it to property that is email ID and account key email ID is the string type and the account key is also the string type so you can add both properties as a type of string in the my class and after that you can use i options but before you have to inject that and configure that service in your program.cs file by using the builder.service.configure method and in the configure method you just pass that class and after that you just get the access section of your app settings so string email ID and account key and in your uh program.cs file you have to access so first of all configure your service so Builder dot service dot configure and the configure method you can use and in the configure section you have to just pass my class okay and in that method just you can use configure dot get section like Builder dot configuration.get section and the section is app settings so uh go for the app settings.json file and that that section is app settings so pass it here in the parenthesis in double quotes okay so everything is fine so how to access that uh properties of your my class in your controller so that is very easy you can use i options as a dependency injection so in which I options you just pass my class as a template so by using the template you have to just access the value of your template that is the property is value and by using the value you have to just pass you have to just access the email ID as well as the account key so just resolve that issue by using Microsoft dot extensions dot options also initialize that dependency in your Constructor so first of all select this and add as a parameter in your Constructor of your controller class so uh guys welcome in my Channel first of all and uh if you like this video so please hit the like button and subscribe my channel also uh don't forget please subscribe please press the Bell icon if you want to get latest notifications and uh latest project of in asp.net core and dotnet Technologies so by using your options dot value dot email ID you just get the email ID as well as your account key thank you guys for watching this video and please don't forget to subscribe my channel uh if you want to act if you want to ask anything about asp.net code just ask me by using the comment section thank you very much for watching
Info
Channel: ASP.NET MVC
Views: 4,630
Rating: undefined out of 5
Keywords: Read appsettings.json file, dotnet class library project, dotnet class library access appsettings, dependency injection, asp.net core
Id: HrPDpD5h0kw
Channel Id: undefined
Length: 11min 42sec (702 seconds)
Published: Wed Nov 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.