BrandGhost

It Couldn't Be Easier! AppSettings.json In Console Applications

November 12, 2024
• 3,658 views
If you're familiar with ASP NET Core, then you're likely familiar with using appsettings.json files for your applications. But... What happens if you want to use a console application and have the same type of configuration? No sweat! We can enable this functionality with ease -- only a few lines of code!
View Transcript
if you've been building asp.net core applications you've probably used the app settings. Json file and you've been able to leverage that through the configuration class however if you're building console applications how can you leverage the same type of thing hi my name is Nick centino and I'm a principal software engineering manager in Microsoft in this tutorial we're going to look at using App settings. Json in a console application just like we would in an aspor app let's jump over to visual studio and check it out to start things off we're going to need a couple of new get packages and I'm going to go over to the project file I'm going to go to manage nougat packages here what I'm going to do from here is search for Microsoft extensions Json configuration and we'll notice right up at the top here we have this uh nou package microsoft. extensions. configuration. Json and it has 1.53 billion downloads at the time of making this video so a whole bunch and if you have a close look at the dependencies in the bottom right here you will notice that it will pull in these other dependencies as well so if I go head and install that this will be everything that we need that's an external package but it is from Microsoft now if I go over to the project file so let's go check it out we can see that we have this package reference here now this one is version 8.01 at the time that you're going to try this out for yourself there might be different versions available so don't worry too much about the version same principles are going to apply now if we go over to our program.cs file the way that we're going to want to leverage this is with a configuration Builder so I going to new up a configuration Builder and what I'll do from here is just use the using Microsoft extensions configuration so we'll get that added up at the top and what we need to be able to do it looks like co-pilot's giving us a bit of a cheat code I'm just going to press Tab and see what it's suggesting so there's a couple of things that I don't necessarily want but that first part looks pretty good specifically add Json file and then app settings Json and then we want to build it so technically this gives us an eye configuration and this is going to give us an eye configur I'm just going to call it config and that's going to come out of the build call here so at this point we will have a configuration reference but there's one small thing that I didn't do yet and that's that I don't actually have an app settings. Json file so if we look over in the solution Explorer on the left hand side we do have the program.cs file but there is no app settings so let's go ahead and make one and we're going to add a couple of very simple things into there so I'm going to go to new item here what I'm going to do is just select file and I'm going to name it app settings. Json I believe in different versions of Visual Studio you'll have different templates to select from I think in the preview version that I've been using more recently you can actually type app settings. Json and it will go add one very simply for you so I'm going to add this in here and you'll notice that we now have an app settings Json file but there's no Json so we will start things off now in app settings. Json there is different ways that we can structure the app settings so one of the things that we can add in here that's built in is connection strings you'll notice that I just put double quotes I typed C and connection strings is already offered up to us so kind of interesting this one had a built-in server this one had a built-in connection string just from uh co-pilot which is interesting so we'll just add that for fun we're not going to use this for anything we're just going to print it out to the console so that we can see that it's working so we have connection strings with a sub item there there's logging again I'm just letting co-pilot put in some data for us and maybe what I'll do is I'll say like our setting and we will put it to hello from Dev leader so now we have a handful of different things in our jsaw file that we can work with and what I'm going to do now is go back over to program.cs let me just double check again we had connection strings and default connection so something that we could do is we could get the connection string out so I'm going to save our uh connection string and thank you co-pilot for doing all of the hard work for me but you can actually use get connection string as a first class method call on the configuration and then ask specifically for the default connection I wanted to show you this one because you do get this access and it looks a little bit different uh than the other thing that we're about to go look at and that's just because connection strings are treated as a first class entry on the configuration file before we move on this is just a reminder that I do have courses available on dome train if you want to level up in your C programming if you head over to Dome train you can see that I have a course bundle that has my getting started and deep dive courses on C between the two of these that's 11 hours of programming in the C language taking you from absolutely no programming experience to being able to build basic applications you'll learn everything about variables Loops a bit of async programming and object-oriented programming as well make sure to check it out the other thing if we go back to app settings. Json I'm going to skip the logging for just a moment but we're going to look at our setting instead because this logging approach is going to be similar in terms of how we access it but we're going to have to parse it out if we're interested in that or convert it to a a data transfer object like a record that will map to this I just don't want to spend the time doing that I just want to show you getting a simple string back out our setting is the name of it so if I wanted to do VAR our setting and then config our setting here so now we have access to both of these things I'm going to go print them out to the console so now we have both of these things here when I go press play can check out what the output is but uhoh this didn't work what's going on with this it says file not found and it said the expected physical path was and then some path of my computer so it's looking in the bin directory then it's looking where we're running from it it's looking for app settings. Json but why the heck didn't that work right we have app settings. Json in the solution Explorer here and the answer to that is that if we go over to the properties of this app settings. Json it's not actually being copied into the output directory this is still in the code folder it's not in the bin directory where we compile to so if I change copy to Output I like putting to copy if newer that way it only gets copied if it needs to get copied so if we go run this now what should happen is that in the bin directory we will have an app settings. Json file so that's going to allow this to complete and then we'll be able to go pull the settings out and print them out to the console there we go our connection string printed out this is the one that co-pilot made up for us which which is nice and convenient we're obviously not using this it's not a real server and then our setting was the one that we defined ourselves it says hello from Dev leader so this first one is coming from this get connection string call and this second one is just coming from the config accessing it by the our setting key and that's all that you need to do to be able to access app settings in your console applications and this is just a friendly reminder that when you're doing this kind of thing you want to remember to not include your app settings in your git remember a lot of the times in our app settings we have things like secrets we have connection strings with passwords or login information uh different things like that that you don't want to have communicated out to anyone else if it's in a repository especially if it's public that's a bad idea but even ones that are private if you want to try to keep that stuff out of the repository as much as you can have it in a controlled Secrets environment now in order to do that you'll want to make sure that you add app settings. Json to your G ignore and there's variations of app settings like app settings. development. Json app settings. production. Json just make sure that you're not including those in git and that way you're not going to accidentally commit some configuration that may have secrets I hope you found this helpful and I'll see you next time

Frequently Asked Questions

How do I add the appsettings.json file to my console application?

To add the appsettings.json file to your console application, you can right-click on your project in Solution Explorer, select 'Add', then 'New Item', and choose 'File'. Name it 'appsettings.json'. If you're using a newer version of Visual Studio, you might even be able to type 'appsettings.json' directly to create it.

Why is my appsettings.json file not being found when I run the application?

If your appsettings.json file is not being found, it's likely because it isn't being copied to the output directory. You need to go to the properties of the appsettings.json file and set 'Copy to Output Directory' to 'Copy if newer'. This ensures that the file is available in the bin directory when you run your application.

Should I include my appsettings.json file in version control?

No, you should not include your appsettings.json file in version control, especially if it contains sensitive information like connection strings or secrets. Make sure to add it to your .gitignore file to prevent it from being accidentally committed to your repository.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload