On .NET Live - Configuration in .NET 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] do [Music] [Music] [Music] um uh me [Music] [Music] [Music] [Music] [Music] uh hello david i thought you were having a seizure there i was concerned welcome everyone this is the august 15th installment of on.net live where our mission is to empower you the.net community to achieve more i'm your host scott addy and with me today our co-host myra wenzel and david pine super pumped to welcome today's guest chris chris ayers um chris funny thing we learned minutes ago that you're actually a microsoft employee um but can you tell the audience a little bit more about your role here and who you are sure um so i've been i was a microsoft partner for many many years and i started speaking and getting involved in the community when i was in that role um i joined microsoft about nine months ago and i actually work for the azure engineering part of microsoft um in the fast track program so we're part of the customer experience program so fast track is a way to de-risk and scale customers implementations on the azure we give you know guidance and best practices we act like your your best friends who are cloud solution architects that'll point out all the ways to secure your your deployments make them more reliable and more scalable and um yeah and i'm a huge devops fan very cool i know when you say devops uh what what's your favorite devops type tool well i did tfs and azure devops for many many many years when i saw github actions in 2019 at universe i jumped in with both feet um so i've been doing a lot of stuff with github actions i actually give a number of talks on it and we recently did a learn live on github actions so i'm a huge fan of of that platform it's so open and there's so many ways to integrate with other services and there's so much reusability and it's just an amazing platform all right well thanks for sharing that chris now that we know who chris is my understanding is today we will be talking about net configuration i also understand mr pine here has a doc related to that topic so with that let's roll into the related bits segment awesome thank you so much so yeah today we're going to be talking about configuration and net now i'm going to kind of preface all of this with the fact that chris is going to do a deep dive he's going to cover a lot more so i'm going to gloss over this article here but you will find a link to this scrolling across the bottom of your screen very soon um so configuration.net uh is powered by configuration providers so there's this premise of you know getting configuration from some sort of source and that's provided through you know files on the the file system like you know json ini xml or environment variables or azure key vaults or command line arguments or third-party providers all sorts of different ways to where you can have configuration and one of the key abstractions is all of these different sources feed into the eye configuration type so you might be asking yourself well how do i get a hold of this eye configuration type so if you're using a simple console application one of the most common ways is through the hosting package so you would create a host given your arguments and then from that you would be able to use dependency injection to resolve a configuration object uh and that's that's kind of like the bare minimum there's other ways to do this as well you could use instead of the hosting package you can use the configuration package there's various mechanisms for binding to the different types and this article will teach you all about all of that and again this is just kind of the preface to how chris is going to pitch and sell configuration to all of the potential buyers out here right so to speak people who are interested maybe you're not familiar with um configuration and that maybe you're used to some of the net framework ways of using um configuration but uh the the the.net uh core and now.net five and six plus are a lot more first-class citizens you know speaking in terms of like using the options pattern and having strongly typed objects represent configuration and binding hierarchies and it uh really goes a lot further so hopefully you'll check out this article but more importantly stick around for the hallway track which we're going to jump into right now sweet so i say all that just to kind of set the stage for you chris now the floor is yours to go in depth and i know you have some slides yeah but we'll skip over them real quick but uh sure yeah so dot net configuration in depth i mean there's some ways you can track me down on github and twitter and stuff but um i'm just gonna fly through these so we can get to the demos and the real stuff but when we talk about configuration we're usually talking about settings maybe some feature flags maybe secrets though i usually try to talk about secrets completely separately because we want to handle those in a different way we don't put secrets in our config files we're going to use a key vault or or something like that most likely or managed identity so we don't even need passwords so we're going to try to do that stuff separate another time but you know background colors retry times you know what percentage our flag is set those are configuration values that we usually do maybe a path and now there's a couple of ways we can deal with configuration we can bake it in at compile time and and i know a lot of us have seen this pattern where we had branches for each environment and we had configs checked into our branches um i i am not a fan i i don't like building things multiple times it is it lowers your trust in what you're doing because branches can diverge and you're testing and running different bits like what you're building in dev and testing in qa and running in prod are different bits and so there's low confidence in what we're doing so i'm very big a fan of build once deploy and run many times where we apply our configuration at run time not build time so and the reason i bring that up is a lot of times in dot net um we were dealing with key value pairs and we we we we were doing these config files let me let me just break out of this i'm gonna bring up visual studio for a sec anyone remember these web configs where we had our debug and release maybe we installed slow cheetah and we we had to go in and understand xdt transforms remember remember this we're like doing this transform language yeah i remember that vividly yeah we don't want to do that we don't want to do that and when we wanted to access our our configuration what did we have to do we had this configuration manager class it was static so it was hard to test and interact with we we everything was a string so we had to um do converts by hand and it was easy it was really hard to unit test it was easy to like check in the wrong thing like we did stuff like this where we would kind of wrap stuff and then like return things or return parses remember that i'm sure you did oh yes i ve yes i wanted a bull out of the configuration system i first had to cast that from uh or convert that from a string yup so so how does it look like today in.net well you hinted at it um there's a whole new set of abstractions that are are there so we've got our eye configuration sources like you said they could be files like json they could be environmental variables they could be command line arguments they could be a custom provider we can make our own providers um azure has done that with azure app config and key vault they've made custom configuration providers so that we can reference those sources when we have providers they're defined in an order and they're done from top to bottom like first second third and then the the configuration builder takes all those providers iterates through them squishes it down and then you get a configuration so that looks like something like this where maybe our json file defines environment and database but our environment variable defines a different database and maybe we have a command line argument that defines a different key completely a log level well when the builder does stuff it just kind of it takes the last in wins essentially so localhost was defined in the json file but it was overwritten in environment so that's what i'm gonna get why is that important though chris well this is where you know instead of having to check in a file to change our config we can take that same build run it in a container and just give it an environment variable we can override it with a command line argument in azure if you're running in an app service and you go to the configuration page those get injected into your app as environment variables so they can override anything that exists in your config files so you're saying basically wherever our code is executing in that environment for example it would have what settings are pertinent to where it's running exactly perfect awesome yeah so um another interesting thing about the new configuration system is we can have all sorts of nested stuff like we could have objects objects inside of objects but when you get into the configuration system and you're trying to access stuff everything's kind of flattened into like a key with like colon separating them instead of slashes and so this is kind of consistent whether it's coming from a json file an ini file an xml file or wherever you're going to do like key colon key colon key to do nesting and then does that make sense oh yeah um another cool thing we can do that we couldn't do in the old stuff is we can have an object so we could we could make an object that we're going to define that has types that we can then say hey hey configuration give me one of these objects for that section like bind these things together and i'm going to show you some examples of this we do have a question real quick um yes i might interject so fuel sample is asking they've wondered what would you do or how does it handle if like there's a key that actually contains a semicolon um i would say that if you're having a key with a semicolon um don't do that that's gonna be an edge case and probably don't do that like sure let's figure out how to break the system let's put special characters in our key value like maybe don't don't yet like that like that that's an edge case there actually something i'll bring up here for our friend fuel snable while we're talking about this there actually is guidance um in the azure portal if you're using something like key vault as an example it will tell you do not use the the colon the configuration provider will do a string replacement under the hood it'll replace i believe double underscore yeah that's the thing that happens um that i was going to show in a little while for um how you could do environment variables and stuff with that but so if i do a like out of the box if i do.net new console guess what i get nothing no configuration management like you implied now if i do net new web app i get some stuff i get like json uh like in app settings json and app sayings json my environment name secret providers environment providers so we got like lots of different type of provider there's i want to get to the demos so um there's all sorts like i said there's json xml environment variable command line there's key per file maybe you want to maybe in a containerized environment map some sort of config store to a file system and and mount it in your container you could do that and point your app at that maybe you want to do testing and you want to do it in in-memory configuration provider so you can dynamically change your configuration between tests so and and listening to you kind of pitch this chris it's making me wonder because i never have really thought about this what can you not do like what are the limitations like i'm kind of oh well there are some uh there's definite ones where you want to try not to repeat yourself on some stuff but um let's get to a couple of basic demos let's do that because i want to get into some code so net new what do i get i get nothing no configuration this is what i have so how do i maybe i don't want to say hello to the world maybe i want to say hello to twitter or the internet how would i do that well i could in my project i could add a package to configuration and maybe i want to use a json file so i could add a package extensions extensionsconfiguration.json and i could say i want to make a configuration builder and i want it to look at a json file named config now build that configuration that's not the right one i i even have it i even have it on my shirt in case i forget dotnet run that's oh you're matching it to her hello so it's getting a configuration named greeting and it's getting a configuration named environment i've added json configuration to my console app with a couple of new get packages and a couple of lines of code um i can now have dynamic configuration that that loads and you know and i come here and i can change this and say you know i'm gonna do that i can run it again and now this should change i i haven't changed my my build but i'm changing my configuration awesome well what if we want to go to a step further maybe we want environment variables right there's a package for that we can add that what do i do to get environment variables i add environment variables to my configuration builder like it's super straightforward and i'm flying through a couple of these just because um like like the these concepts build and they're very easy so yeah i'm in i'm in bash i'm i'm actually running in a dev container so i could say environment equals qa and edit a file all i did was set an environment variable i'm overriding things what if i change this to prod but i'm still running my environment well last in wins remember we're doing json first then environment variables so even though i made my config file say prod environment variables wins because it's a clearly defined order and how they're being processed and that precedence is very very important and a lot of people aren't familiar with that so it's a very good point to call attention to and that's why i'm starting with the console because there's nothing and it's very easy to to do this one so nope almost got me again what if we had command line this this is where we start doing interesting things well see look we we can do our command line we can just pass in dash dash let me just make this a little bit higher we can push in run in dash dash the config value name and then the value we want we can also mix it with an environment because environments still work we've got environments right there they still work we can do both and show last in wins so we're giving an environment and we're giving a command line and look we haven't changed how we accessed our configuration at any point we're not doing anything special or different between command line configuration json configuration and environment variable configuration it's configuration doesn't matter it's abstracted away um which one was this one so yeah this is user secrets so this is an amazing feature that was added um that i don't think enough people know about oh i i had some extra references this was a a different demo at one point but this is the important one configuration user secrets um a lot of times you were doing connection strings when we're running in in qa or prod we've got things that we don't want somewhere else and so this is um how the system kind of tracks things when i add user secrets i go net user secrets init and what it'll do is if i don't have this user secret id it'll add one for me and the reason we do that so let me delete it and i'll show you save that um when we want to use user secrets we don't want it stored in a file maybe we don't want it stored in environment somewhere let me close this and reopen it so it refreshes so it added in a new grid this is going to be stored in my uh like app settings uh my app app data local like as part of my user profile so it'll be stored somewhere on my local hard drive uh in a in a config file a secrets file but i can say something like net user secrets set so i can set some sort of value that is going to persist in my user secret thing and i can say hey i want to set the greeting to hey we do have another question here so someone's uh fuel snapple is asking again uh is there a use case for having the same user secret id for multiple projects or should it always be unique so i um you don't pick your user secret you usually when you do an init it gets initialized and it just gives you some persistence i don't feel that sharing it across multiple projects is probably a good idea i mean if you really want to you you have control if you know that you're going to use like multiple front ends or multiple middle services all pointing at the same database or or something like that and you're just using it for testing sure you could point multiple services at the same uh id and it would work um but yeah yeah as you can see we have some other questions now related to this you know uh we have an individual asking uh what about using them on the production server no user secrets are not for um this is only for a development thing um i will show you the documentation on that right now so so if it's only for development environments yes the question might be why would that xml node in the cs proj be source controlled um that would be source controlled um so if i go to another um so if i let's say u we're doing stuff in a net and you had checked in your project file and had user secrets um and for you you were running it and you didn't want to check in appsettings.prod you didn't want to have a script where you were setting environment variables every time they ran you you didn't want to do anything like that and and now i get it and i come in here and i go hey um i tried running it and maybe it fails um because it tries to connect to a database and there isn't one there um you know maybe did i save it oh i'm in the wrong project file that that's why i was doing it in a different project file trying to do a demo on the fly and uh and answer questions yeah so so let you know i pull it down i just won't have any set for me and so if i try running it and it fails because i don't have a connection string i would then it's my responsibility to go hey i need to go set my own user secret to my own local dev instance of whatever right so this user secrets id node in the xml for the cs proj is really just an identifier that helps the system know this is a unique id and so it's not really it's safe to be in source control right is the answer and very much the the individuals who are pulling that are cloning it locally and have it um when they run it if something's not there they would have to manually add it so that it's there yeah so because i initialized it yeah like it's just it's just that it's no big deal um it's very easy and safe to check in and there's there's no issues there um now things get really interesting though as we start playing around with the command line a little bit more um you know we we can you know we talked about nesting you know what if we want to nest our our our configuration we don't just want to have a big flat thing a configuration we want to have nesting structure we want to define things but we got one more question just to clarify the previous comments here so michael's saying it's safe but wouldn't the cs proj constantly be updated by check-ins from different devs no the idea is whoever initializes it it generates a new good and that's the good that's going to sit there for the project so yep and if i come along and pull it right it's not going to re-initialize it the id is already there it's just that as you have it locally you might not have those secrets so then you would have has already yeah has already been initialized right if it already has the key it won't add it again it won't change the grid it's a one-time thing it's just you won't have any secret store backing it that has values in there so you would have to add your own values that's the other thing you have to initialize with that same go goet on your own machine yep and that's it the other thing i'll say here so if you're looking at that value and you're saying boy that's ugly uh i'd rather not use a good uh you don't have to use one it can be whatever value you want so long as that's unique within that user profile folder location the folder name has to be unique yeah and that goes back to fuel snapple's um question earlier really when they were saying hey uh you know would there would it make sense to use like a shared user seeker like the same one for different projects and the answer is no you would want to avoid that otherwise you start can having conflicting projects and you know sharing config exactly you'll also notice if you were to create a new asp.net core web app uh by default the innards of asp.net core do not register the user secrets configuration provider when running outside of the development environment it will specifically look for that asp.net core environment value and if it's anything other than development that configuration provider will not be registered so it's yet again another indication that they do not want you to do this in production yeah and i'm explicitly adding it here in in a console app because it gives me a blank slate to work from so you know now kind of just moving on a little bit we've got nested configuration we've got greeting that has two things a message and a color and we're just going to access that with that colon that we talked about so things are getting flattened down so we're going to run it with that thing and you can you know using command line you can override that so you can use the depending upon the os so some os's might not want you to use the colon that's where um you can flatten it usually i i think on windows you can flatten it with that on linux it still likes the colon that's the kind of the caveat there so it varies but we can override individual things we can access that nesting very easily with just that colon and you can on all these providers you can say these things are optional if you want and this is where the order comes into play right like you have the json but if you pass through command line then it overrides the json and that kind of thing now another thing i was just like just to keep going a lot of times i see people doing all sorts of command line argument processing and they're trying to do things to handle long dashes and short dashes and all sorts of different things uh to handle configuration well there's a mapping capability for for the command line args so i can define a dictionary with a short you know a friendly value and then the more complex configuration object that we're defining that it resolves to yeah yeah i'm still using the bigger thing inside my code but when i'm passing it on the command line i don't have to do that you get to use the alias yeah so i can just say env equals qa and i um i don't have to do dash dash environment i can do color equals blue and i don't have to worry about all of that nesting like built-in don't need an extra library for i think that's cool it is definitely so the next four four demos i've got here i want to show how you might evolve a configuration and start using uh bound objects so we start introducing objects you know maybe we were going to make a tool that processes markdown files you know and so we're we're writing out a whole bunch of flat things i mean that that works i mean we could we can do that it's just very repetitive you know we start seeing some similarities in what we're doing what if we started grouping things you know maybe we did them in a hierarchy you know that now we can kind of be a little bit more explicit about what we're doing we're intentionally saying hey i'm i'm going to do stuff for the pdfs but you know we're starting to notice a pattern here we're doing the same three things for each extension what what if we could do it as a like a method maybe we could process things in chunks and we could start getting some reusability in how we process our code right that would be cool we could have some nice organization so we've still got the exact same config file we're still doing like the same hierarchy of config but now we can do this cool thing called get section okay so the configuration object we can tell it i just want to get this one section and treat this as like my config so i don't have to reference the hierarchy every time i can just say in my code you know i made a little little funny method here i can just say i want to print out the file extension or the output directory so now i don't have that hierarchy i can kind of start abstracting away how my configuration is built now i'm i'm still explicitly dealing with each thing but i've abstracted it to a degree right here's where it gets fun lots of funny characters but should still be okay still prints out the exact same way but now what we've got if we look in this file we're doing a bind to this file options object so what we can do is we can define a class that has explicit values and we can now operate on our class and we can say you know i can do it a couple of ways i can say bind this you know give me that section and bind this object to that section and so it will it'll do all the mapping for me from the config file to the uh as soon as long as the names are matching so now if i wanted to operate on configuration in other classes i don't have to take a dependency on eye configuration right i can take a dependency on my file options so i can exp i don't even need to depend on some of the core configuration stuff inside my code and when would you want to do that a lot of times you would you would want to kind of bind your specific configuration class to your config you know so if you know as you're building your app you know i'm i'm going to be needing file extensions i'm going to need you know as you start building up your configuration start with a class and then maybe define your configuration to match that class i do this a lot a lot like i know what my logic is or as i'm building my logic i start with the class and and you know one of the i think big advantages with that automatic binding is when you start getting into different types beyond strings because when you when you work with like the just like the native i configuration object everything that you're accessing is a string right so you can have bool or a time span or an ip address or a guide or uh you know even nested objects and then it will automatically you know deserialize those from whatever format they're coming in you know whether or not from environment variables or the json hierarchy here or xml or whatever the different source is yeah you can go to these objects and it will bind it automatically if i were to just quickly um yeah yep give the quick and dirty this should still work just fine uh going very quickly live demo that yeah now you'll see that the retry of three flows through as well as the the bull setting true and that was coming from the config value so it just just flows just flows right through that is not a string that is not a string that is not a string so yeah just it picks it up and flows right through but like that that's just super awesome so that you know when we do create builder um you know so that was we did all that in in console apps just to kind of get to where we want to be in asp i come into asp and i do a run you know a lot of times you're going to get a web application create builder you're going to get a builder.build at some point you might say if environment is development it knows about these things these things happen well out of the box this is what gets registered we're getting some stuff registered out of the box um providers we've got some chain configuration app settings we got the environment variable um and the reason and the way i'm getting this is on a page i injected the eye configuration root and i just got a list of the providers so they're listed you can see what providers have been initialized in your configuration um you know i'm just looking through them and printing them out but like this is all open information so i can inject my config i can get configuration from stuff if i want there are a couple of ways we can inject configuration so you know i can inject the eye config and let me go back to my slides for just a minute to show you why you know we've talked about all of these um and we we kind of started talking about this this is where configuration becomes really important how we inject it so yes you can inject denied configuration route in your controllers now all your controllers depend on uh you know configuration extensions um there's some other things that go on so out of the box when you you know with configuration if you edit a file it might not reload you know back in the dot net configuration uh framework days you touched web config a lot of times the whole app would just recycle like it would it would go down it would reload we don't have to do that anymore um you know and but there are some limitations on how we inject things like we can get reloading of our config like we we can get that um they give you some options uh snapshot and options monitor that will let you inject and reload things um but out of the box you know like we can use this eye options thing but it doesn't give us reloading and the reason this matrix exists is depending upon your services and your pages um eye options can get injected into a singleton but snapshot can't monitor can get injected in into other single tins and it can reload and then we also get this other cool thing called named options so let me just show you that real quick and this will start to make sense so you know if i if i just want to inject my config i can just inject my eye configuration i can just inject that i can get sections i can do the get file options i can do all of that that can all be injected i can also do i want to inject this options class itself so i made this markdown converter this is the the class these two classes are the class that defines my configuration so i've got a you know a hard-coded options class that that will be bound and so in my settings here if i say hey configure this class tied to that that's how i enable that options injection anytime something asks for a eye options of a markdown converter or whatever the configuration system is handling that behind the scenes that mapping it does it for me for through dependency injection so i don't have to inject the whole configuration thing i don't have to get the section i don't have to do the binding i don't have to do any of that i can just say i want one of these we've got a question real quick here so we have an individual asking when is it um better to use eye options pattern versus binding so this essentially is binding where we're taking the whole configuration thing and we're doing some binding ourselves i think eye options is typically better if you can like because you're letting the configuration and the dependency injection themselves handle all of that behind the scenes otherwise you're going to be down here explicitly binding things and explicitly injecting things and it just adds a layer that you don't really need right so the binding mechanics are really an implementation detail of how the options pattern happens yes yes exactly and i do have you notice i do have a service here i'm going to show you a little bit about some of the injection stuff so you know maybe we've got our um our eye options monitor maybe we've got our eye option snapshot and then maybe we've got a service and our service if we go look at our service just real quick our service it's taking an eye options monitor markdown thing so then the reason i'm showing these off is you get some different behavior so can i can come in let me just come into my config real quick i'm gonna make a change i'm gonna say this is docx not doc i'm not gonna restart the app yeah i'm still running so i can come in and like poke around so if i do the injected config this is now updated so the injected config um is getting me the latest thing if i look at my eye options like part of it didn't update like like it it it's getting like you're not getting all the live updates at the exact same time um but you can get some dynamic updates but if i look at my um and so this is this is a cool way where you can start having dynamic config come out of your stuff but but as you start changing your services as you start changing how things are injected you might start getting um issues you know so if i was so i was using uh transient and you know or if i go back to singleton and i want this to use instead of an eye options monitor i want to use an eye options snapshot no it would give me an error as i tried to invoke it just because some of these services don't work with singletons and don't like support reloading out of the box um you know we got a couple more things to talk about like um azure app config and and also i haven't gotten to names name things yet so what if you know we're dealing with this and we have multiple file options and we want to be able to dynamically loop over objects like maybe we don't know how many of these things exist maybe we want to loop through them or we want to inject them you know have you ever had dependency injection where you have two or three different choices for an implementation and you want to get one out of them so that's where these name things come in and not all of the injection patterns support name stuff but this is where you can say hey i'm going to give you a file options but it's going to be named pdf and it's going to be tied to this section of the thing so if you ask for the pdf one you'll get that file option but if you ask for the doc one you'll get a different one and the way that works is um you know so here i'm doing the config injection here i'm going to do some injection in razor so you can actually do the configuration and direction right in razer because you can do that if you want we can try doing it with eye options and and notice it's just uh i opt file options there's going to be like multiple and it doesn't work with naming like you just get the first one every time you do an eye options but with the snapshot in the monitor you can say give me this specific one give me the pdf give me the doc give me the html one the same thing works with monitor so oh i need to stop it and start the next one but name named options is really cool because that gives you a little bit more flexibility and you can code things a little bit more dynamically and and not hard code uh things the exact same way okay and that's how we're there and that's another reason why you would consider using the options pattern instead instead of like manually binding yeah and manually hard-coding stuff right um yeah those might not work but here we can get the exact name ones we're looking for you know when i asked for pdf and i asked for doc they all they all came through um and yeah doing injection in razer works just the same as doing conjecture in a in a controller so all that works um and and i wanted to add this too real quick because you know as i work on the.net docs um i'm not focused on like the asp.net docs and i know that you're kind of blurring the boundaries here a bit where you're talking about controllers and stuff but i wanted to be clear that this is uh not just applicable to you know web workloads it can be literally anything it could be workers it could be windows forms applications it could be xamarin uh maui uh you know yeah any any dotnet workload you can imagine iot scenarios whatever it is which is why i started with console apps just to show we pull in those couple of of nuget packages and we can enable configuration in pretty much anything with just a couple of lines of code um so one of the the cool things that i've started using quite a bit is um azure app config so um azure app config you know that's a that's a great service out in net in the cloud so you know this is a service that has feature management as well as configuration management so i can have a gui here and i can you know edit and and change my my values on the fly and save it and i would maybe want net to pull that dynamically well one way i can do that now this one is we're still doing the asp we're still creating a builder and after the builder is created i'm going to pull my connection string out now this might be in my my user secrets that this could be um you know an environment variable but we're going to configure app configuration and we're going to access the builder which which we're going to be able to get and then we're going to add in that connection string and something else you can do is you can tell it to dynamically refresh like like go and check your settings and refresh your settings uh you know every five minutes or five seconds or 30 seconds and so you can have some dynamicism where it'll refresh those things um and and just like before once you've got it in there it's all the normal stuff like if i want to access these settings am i going to my my controller uh it's probably under my views yeah it's under my views and i can just access my configuration so this is another way i can inject it if i want to and just pull those settings in so we've got a lot of different varieties on how we can interact with i think i this my uh configuration set if not i might need to to set my user secrets we'll find out in a sec yeah i don't have my connection string set so uh look i have no secrets yet so um i have all this stuff set up so i blow it away and recreate it a lot but what i might do is uh get my access key copy my connection string and i can come over here and go set my user secret connection string off camera of course for for wonderful safety purposes i was just making sure i had the right name we've got a question while you're doing that um ryan's asking does this work with blazer as well absolutely absolutely works with blazer and like you said works with xamarin works with iot works with all sorts of stuff every dot net workload imaginable oh yeah someone was asking about winforms as well um yeah this works with winforms uh yes i think right must have set it to the wrong see connections oh all right okay i didn't give it the full key name that's why sorry about the little delay there no worries yeah there we go that that should be better now i just put can uh apps i just put app config not um connection strings app config so see we're getting this data from our app config now and i can uh change that if i want to so i can go down here to the configuration explorer i can change my message say hello [Music] while you're doing that we've got another question does it work with a service worker running as a windows service yes in fact i've got docs on doing that as well i can share yeah all that should happen so you know we so you know there's lots of options there's great docs on setting up app config uh another thing and i'm not going to dive too much into it because i want to make sure we save time for questions and stuff but um yeah keeper file that this is where you know we can add a directory for keeper file um and then have you know logging key file we we can we can have different values uh there if we want we can do key vault so key vault you can you know add key vault in now this is a console app and so i didn't have the builder build to get the the name but once you add in key vault you can use manage identities if you need to you can use certificates or something else but manage identities work just fine especially if you're running an azure and then in asp instead of like hard coding things like i did kind of on the command line we have more flexibility where we've got that builder you know we can we can get configuration out of the builder a little easier maybe we want to have a different url and use that that managed identities when we're in production and then you know we can access our secrets just like we did any other config we can do nesting we can do all of that we can get a section and then get secrets so we have so much flexibility in how we access configuration and manage it we did have someone ask a question here about whether or not they can use yaml and i just wanted to say that there is not an explicit yaml provider however fun fact all valid json is also valid yaml yes and there is also this little link right here would you like to implement your own json provider well you can implement a custom configuration provider and net they give example using entity framework on how you like your models that you're doing and how to build your own like configuration source and provider like like they give you directions on how to do this stuff so if you would like to build your own configuration provider that takes yaml that'd be awesome like like do that that's cool um all of this is here uh in the configuration links that were shared before you know more detailed guidance around the options pattern [Music] and di the whole api on it some guidance especially if you're a library author so naming conventions yeah namespace guidance i just you're just pulling at my heart strings here chris cause these are these articles are things i've written so that i'm just scared hey i wore my shirt today because i'm a fan okay this is just a tribute we have an interesting question in the chat um that's i think it's worth addressing here there's there's a lot of chatter about user secrets it seems you've made a huge impact with so let's assume i'm a dev i'm using user secrets on my local dev machine and now when i deploy to production i would like to migrate to using something like key vault in that particular environment how would you recommend proceeding so um i actually think there's some demos of that um that happen so so like your config you're just referencing values so if they are coming from your user secrets so so in production you've got an sql password and an sql user and they're stored in key you know you would not point your production instance from your dev environment but in your dev environment you want your stuff to look the same so you might have a local sql server you might have a local sql admin and password that would be in your user secrets in your code you would still reference the configuration values the same way how you interacted it would be the same way if i had a connection string it would be the same way so just because locally it's being served by user secrets and it has just some sort of identifier in the cs proj that user secrets file does not get checked in it is stored somewhere else completely um but it's how your code is defined in your code how you access those connection strings those api keys those all happen behind the scenes so whether it comes from um key vault whether it comes from user secrets that's fine um i do believe like like there's some sample apps out here around key vault and if we come in here and look at some of them if it's production it's getting key vault well what if it's not production you know if it's production it's getting key vault what if it's not production it just gets these values so what if i were to say uh secret name in my config in user secrets let me just copy this file and let's see what we can do you know boom boom copy we're just going to have some fun i'm just going to make a folder and and do this since we got a few minutes while we chat why not you know live dangerously i like philippines and in terms of like i'm curious um on your role or on your experience like from people migrating to framework to donate cordon at five plus like what are some of like common mistakes that you see in terms of maybe keeping old configuration patterns or like not doing something oh that's a great question so um what i see with a lot of people to be honest when we help them migrate i used to do this as a partner i do it now um you know for microsoft but let's say for instance that someone has been in.net forever but they're currently on legacyapps and.net framework they've probably had to add or roll their own dependency injection container they probably had to add or roll their own configuration library or they're just used to doing xml config transforms in web config or they're maybe putting it in a database somewhere there's lots of options but there's fundamentals that were developed in dot net core that are now intricate parts of net five plus five you know six seven on how configuration is handled how uh logging is handled all of the how dependency injection is handled so i would um reassess the patterns and go look at the guidance that that's out there the documentation is phenomenal so um let me just run this before i set any secrets so if i do a dot net run i have no anything oh let's see what it's complaining about cannot be found dotnet oh i i know what i i i know i i didn't add the identity in the x 509 and stuff like that i love that you're using the command line to you know walk up the dotnet cli and say add packages mere mortals just use the you know gooey he did say living dangerously yeah and i don't remember some of them that's why i'm just coming up here and i was gonna like uh i wanted to use your secrets that's what i wanted so see what blows up now okay so [Music] x 509 stuff from a string eye configuration is not defined so i did that one i didn't do that one i'm just doing this as we chat i'm sure there's more questions coming in yeah yeah we're just pressing buttons and hoping for the best uh it works for me a lot of times yeah oh i said net new console didn't i yeah i did so you have to change the sdk maybe at the top no i i think i gotta add these why don't you just grab all those package references and then just paste them into the other project uh look at you being smart actually let me do this one and see if there's more in here yeah i'll just add these uh being smart why why why you gotta be so smart uh you're gonna ask myra and scott they would attest that i'm not well it would never um it does not exist in the current context why why uh i'm not even seeing that so go back oh web application create builder yeah i didn't grab the asp stuff there's some asp there we go restore yeah who restores these days okay value cannot be no there we go that's a little bit better program is 17. so it was trying to get this one from a environment is production so i just need to set my environment to develop all right actually i'm just gonna remove this block don't feel like doing that right now and then after this we're gonna have to call it we're approaching the top of the hour so yep so if we run it um there's nothing there so if if you define a config and it it can't find it you pretty much just get empty string or null but let's say instead we come here and we do net user secrets set keeper name is chris we run it again oh i did i have an extra colon i had an extra colon didn't i i had an extra colon all right i'll let you wrap it up then sorry all right uh thanks chris um so i'd like to thank all of our viewers for your loyalty and for tuning in to today's episode of on.net live i've seen a lot of questions about this so i'll call it out right now we do record each and every one of these uh fantastic episodes which you can find out at youtube.com slash d-o-t-n-e-t also do check out our other uh live streams focused on dot net those can be found out at d o t dot n e t slash live tune in next week we'll have guest david mccarter joining the show to talk about code quality and performance as it relates to.net see everyone next week and thanks again to our guest chris for all the awesome information today awesome bye friends thank you for letting me uh live dangerously at the end [Laughter] all right bye folks bye bye all thanks [Music] wow
Info
Channel: dotnet
Views: 14,989
Rating: undefined out of 5
Keywords:
Id: 6Fg54CEBVno
Channel Id: undefined
Length: 63min 57sec (3837 seconds)
Published: Mon Aug 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.