AZURE APP CONFIGURATION | Central Store For Application Configuration | ASP.NET Core Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
azure app configuration provides a service to centrally manage application settings and feature flags modern applications especially those running in the cloud have different components to it and are usually distributed in nature spreading configuration across these different applications and components can become hard and error prone to deploy azure app configuration makes it easy to manage it in a central place and also keeps the sensitive information secure in this video let's learn about azure app configuration how to set it up and also how to use it seamlessly from an asp.net core application hello everyone my name is rahul and welcome back to the youtube channel if you're new here i generally talk about cloud devops and dot net now i'm sure one of this is definitely of interest to you so please make sure to hit the subscribe button without much delay let's head straight into the code and see more about azure app configuration let's head off into my command line and create a new asp.net core web api application i will use the.net cli tool so that specify.net new and web api to create a new web api application this will create a basic template into the current folder let's open this up in the ide i will use rider in this example so let's specify rider and dot to open the project that we just created i have the project opened up in rider so here we have the program.cs the startup.cs and a default controller class which is the weather forecast controller the controller class has a get method defined which creates some dummy data when asked for the weather forecast this currently returns 5 days of weather forecast for demo purposes let's assume we want to control the number of days from an application configuration now depending on your application you might have different needs and different things might go into your configuration files some of the common examples are connection strings api keys urls secret keys and also things like this which can config application behavior so any value that you want to configure for your application can go into azure app configuration so in this case i will simply extract this 5 as the configuration value to show you how azure app configuration works and how to set it up from an asp.net application to start with let's simply make this as where count is equal to 5 which we will now replace it from azure app configuration let's use the count from here and leave it like this let's head off to the browser and to the azure portal which is portal.azure.com to create a new app configuration let's head off to the search bar on the top and search for app configuration now select this from the list which will take you to the landing page of all app configuration inside your subscription we can use the create button up on the top left to create a new app configuration so let's specify the subscription that we want to use so currently i will use my personal subscription let's create a new resource group for this demo so let's click create new and specify a name i will specify youtube demo and click ok for the resource name let's use again youtube dash demo this resource name needs to be unique across all app configurations that's currently configured in azure this is because this provides a unique url to access this particular application configuration so if you are having conflicts make sure to find a name that matches your needs and also unique let's select the location since i am in australia let's select australia east there's two pricing tiers that's offered for azure app configuration they are the free and standard by default it's selected to standard so let's choose free for this demo if you want to see the full pricing details you can click this link here which will show you the differences between the free and the standard version some of the things to note are in free you can only have one resource per subscription whereas with standard it is unlimited there is also storage limitations with free and also the revision history the number of days that's available the most important thing might be the request quota this is the number of requests that you can make to azure app configuration you can see with the free version we have 1000 requests per day whereas with the standard it is 20 000 per hour now once you exceed these quotas it will start returning an error code this is something you need to be aware of and essential to keep the application running depending on how you have app configuration configured we will come to that later in this video you can go through the other differences and also the cost and decide which version you want to use so let's go back to the azure portal and continue creating our app configuration so let's click create and this will create a new app configuration into our subscription the new resource is successfully created so let's click go to resource this takes us to the landing page of the azure app configuration that we just created here you can see the endpoint that is used to access this app configuration the name that we chose is also part of the url that references to this app configuration this is why this needs to be unique across the azure configuration that's available now we wanted to move the count property into the application configuration so on the left side let's navigate down and go to the configuration explorer to start creating a new configuration value so click create and use key value here we can specify a key and the associated value for that so let's use weather call and count as a name for our key for this example i have used weather because this is for the weather application and the count as the property you can use whichever name that is suitable for your application now for the value let's give it as 5 which is the current value that is there in the application you can also specify a label and content type for this demo let's leave this as empty so let's click apply which now creates a new key value into this app configuration so here we have the weather column count and the value is currently hidden in the ui you can click this to see the details now let's switch back to rider to start using this from our asp.net application as you can expect we need a new get package to talk to app configuration so let's go into dependencies right click and say manage new get packages let's search for a new get package which is app configuration dot asp.net core there is a package microsoft.azure appconfiguration.asp.net core this is what we need to install to start using it from our application so let's click the plus button to install this into the current project the package is successfully installed so let's navigate into the program.cs to start using it the azure app configuration is nothing but another configuration source provider for our application earlier in a video on configuration i had showed you the different configuration and the providers that's available for our asp.net application now by default this is configured inside the host dot create default builder so if i navigate into that function we have a configure app configuration method that is being called this sets up different configuration sources for our application by default here you can see it sets up the appsettings.json which is why we get all the values that we define in the json file as part of our configuration object it also sets up optional appsettings.environment specific json files it also uses secrets which is coming from secrets manager environment variables and command line variables as well now these are different configuration sources from where we can look for configuration values for our application now the azure app configuration is yet another one in this whole list so what we need to do is add a new configuration provider for this application so let's go back into program.cs to add the azure app configuration inside the web builder let's call the web builder dot configure app configuration which is a dot net framework method on the i web host builder to configure the app configuration so let's call this method which expects a configuration delegate so let's pass a delegate function there and start configuring this configuration object inside the delegate we can use the config object and call the add azure app configuration method which comes from the nuget package that we just added this takes in a connection string or a configuration options delegate to configure this particular connection to start with let's use a connection string which we will replace later on in this video so let's specify connection string and define a new variable connection string inside here now this value is configurable for your application so let's get this again from the configuration object so we can go to appsettings.json and specify a new value inside here so let's specify connection strings and specify app configuration as a value inside our program.cs to retrieve the value that we just added in appsettings.json let's use config.build which will build the configuration providers that's currently there in the pipeline which will not have the azure app configuration because we haven't added it yet and then we can get the connection string from that so let's call connection string and specify app configuration which is the value that we specified here so let's copy this navigate back and paste it inside here let's move this wire connection string to be inside this line and use it directly from there so we can remove this line so this will have the connection string that is defined inside the app settings.json now this currently works because it's able to read it from the app config which is already configured by the create default builder method to set up the connection string value inside our appsettings.json let's navigate back to the azure portal under the access keys we can copy the connection string that's required so if we copy this primary key we can use that from our application now this does have sensitive information because it has an id and a secret so if you were to navigate back and paste this inside our configuration file you can see all the details that's part of this this has an endpoint which is the youtube demo azconfig.io the id and also a secret that's associated with this connection string now i normally wouldn't leave this inside an appsettings.json but instead create a secrets manager and save that in there for local purposes if you're new to secrets manager you can check my video here to learn more about it now i will be showing how to use managed identity later in this video that you can use to move away from this sensitive information inside your application configuration but for now to see how this works let's keep going with this connection string now coming back to our program.cs we have the azure app configuration setup and we also have the startup class being called inside ourprogram.cs to start using this value from azure app configuration let's navigate to the weather forecast controller and inside the controller let's inject in an i configuration so let's use i configuration and specify configuration in here let's also add a backing field property so that we can start using this from our get method now instead of hard coding the five let's specify underscore configuration dot get value specify this as an integer type and pass in the value as weather column count because that is the property that we have added inside azure app configuration let's put a breakpoint here and run this application to see if this works as expected the application is running successfully by default it opens up the swagger endpoint because the web api is set up with this let's go to the weather forecast endpoint use the try it out method and click execute to call this method it hits our breakpoint and we have the count value 5 coming from our configuration now this value is getting retrieved from azure app configuration because we had configured it as a source inside our program.cs let's resume the execution and this will return the values as expected with 5 values inside of here if i navigate back to the azure portal and go to the configuration explorer and update the weather count values let's click on the dot here and click edit so let's specify 10 and click apply if i come back to our application and run this endpoint again this is still going to give us 5 records so if i put a breakpoint again on the count property and run this we can see that this is still retrieving the value as 5. this is because currently the app configuration values are not refreshed whenever the program starts up when it hits the program.cs and when we add the azure app configuration this is where it makes a call to the app configuration and loads all the configuration values from there into our application now this is done only once at the app startup so any changes that we do to azure app configuration needs an application restart currently however you can fix this so that it starts working automatically as well so if i stop and run this application again then the new value 10 will be picked up by this application because it hits the program.cs again and reloads all the app configuration values i have the application running so let's click try it out and execute again now this time when the breakpoint hits i have the value 10 that's coming in here which is the new value that we specified however when building applications we want these values to be reflected in real time we don't want our applications to be restarted so let's see how we can configure that with azure app configuration in an earlier video on the options pattern i had showed you how to automatically reload values using the i options snapshot now if you're not familiar with the options pattern i highly recommend checking that video out to start using options pattern let's create a class to hold our configuration values let's call this weather configuration and add an in property which is count for now there might be more values for this configuration but let's keep it just count in this case let's come back to our weather forecast controller instead of taking in an eye configuration let's use i options snapshot which is an interface that's available as part of the dotnet framework and specify whether configuration that we just added so let's update this configuration property as well to use this i option snapshot of weather configuration so let's replace that instead of the get value method which no longer exist on the i option snapshot we can use the dot value and then get the count from that now since we have used a different interface to be injected in we need to configure this in the startup.cs class so let's navigate there under the configure services method where the dependency injections can be configured services dot configure and specify the weather configuration and specify this will be coming from the configuration object now i already have the configuration object being injected into the startup class using constructor injection so let's use that and get the value from there so let's specify configuration dot get section and specify weather as the section now this is because we have the configuration values as prefixed with weather value so we had the value inside our app configuration as weather column count so when we get the get section of weather it will check for all the values under the weather section and bind it with this weather configuration class now since the names match up inside the weather configuration with count it will automatically map this from the configuration values let's navigate back to weather forecast controller put a breakpoint inside the configuration object and run this application to make sure this is working as expected let's go to weather forecast and execute this method to see what happens the breakpoint is hit so let's step over that and we have the configuration object that's coming up here and we have the value of 10. now the options snapshot way of injecting and reading the value of configuration is working fine however we have still not instructed the application to reload values from azure app configuration to do this we need to do further setup so let's stop the application navigate back to startup.cs and under the services method let's call services dot add azure app configuration method if we navigate into this method definition we can see this configures a i configuration refresher provider which is of type azure app configuration refresher provider this is used to refresh the configuration values inside our application pipeline now by calling this inside the configure services method we are just registering it inside our dependency injection service collection to start using this we need to navigate to our configure method and call this here as well so we can specify app dot use azure app configuration to start using this inside our middleware pipeline now if we navigate into this method we can see this gets the configuration refresher provider and uses that to refresh the app configuration now this is configured within our application middleware so this runs on every request if you are new to application middleware check out the video linked here or in the description below by default the azure app configuration provider is not set up to refresh for values so if you navigate to the program.cs where we initially set this up inside a configuration provider we can now set this up to refresh for values as well so instead of simply specifying the connection string let's use the other overload which takes in a configuration option as a delegate so let's remove the connection string specify an options property and define a delegate inside here now using the options property i can specify the connect and pass in the connection string as before we can now use the options.configure refresh to set this up for refresh now this takes in a delegate as before so let's pass in the refresh object and configure what values to be refreshed so let's specify refresh dot register and specify a key that we want to refresh for in this case this is weather call and count key so let's simply specify that which now configures this configuration provider to refresh this value now this uses a default time period to refresh these values you can also configure a custom time by specifying the cache timeout on this registered object so let's call the set cache expiration and specify a time span value so let's say time span dot from seconds and specify 5 this means every 5 seconds this value is going to be checked from azure app configuration for any updates let's run this application to see if this is now working as expected the application is running so let's navigate to weather forecast and execute this method we have the breakpoint hit let's add a breakpoint on the count method and resume the execution so here we have the value of count as 10 that is currently configured in our app configuration so let's resume this execution and it returns back the results as expected now if we navigate over to the azure portal go again to weather.com and let's edit this value and specify this to be 2. let's click apply if we come back to our swagger ui and execute this this still has the value 10 because 5 seconds has not passed so let's continue this execution and try making a request again after a few seconds so let's click execute and this time the value should be 2. this is because we made another request after the 5 seconds which is what is configured in our program.cs to refresh these values on so every 5 seconds it will hit the app configuration and refresh these values from there let's come back to our weather forecast controller and let's say now we need more configuration options inside our application so let's say we want to hit an external api to get real weather forecast data this might mean we need a url and an api key to hit these applications now i had shown you in my http client video on how to set up external apis to retrieve values from this function for this example let's simply go to the configuration value and add two properties to see how that would work so let's specify public string and specify a url value let's also add in a property of string for the token that we will be using to hit the url now since we have these two values defined here let's navigate to our azure portal and create two new values as well so we need to use the same convention so let's say weather because this is the configuration section that the application looks for so let's specify url let's specify a dummy url so let's say my url and click apply let's add a new value let's say weather colon token and specify my token in this case now i'm not going to actually hit a url which is why i have used dummy values now with these values configured let's come back to our application and put a break point and see how this is working so let's run this let's execute this method the configuration object's value now has the count the token and the url properties now this has automatically come into our application without needing to change much we just added these properties to our options configuration object it has automatically picked it up from our azure app configuration if we navigate back into azure app configuration and if we were to update the url let's say so let's go and edit these values and say my url new so let's click apply this value is however not going to reflect in our app configuration currently this is because in our app configuration under program.cs we had only set it up to refresh weather call and count so this is not looking at the url property currently so we could go and add in a new refresh dot register and specify the weather column url property as well if we follow that pattern we'll have to keep adding new values every time we add a new property to our app configuration since this is not possible and it is also not effective because if you remember from the pricing strategy of app configuration it is depending on the number of requests that you can make so we want to keep the number of requests to app configuration to a minimum now app configuration provides a concept of sentinel key which is a special key that you can update after you complete changes to all the other keys now this is like a master key that you can use to refresh the configuration values for a particular application so if i come back to the youtube demo and create a new key let's say call this refresh all and specify any value that you want you can either choose the date timestamp on which you have refreshed a value or any random value that you can keep changing so let's specify one for example and click apply now coming back to our application let's configure this application to refresh based on that key instead of weather call and count so let's specify refresh all because that is what i had used inside the app configuration we also need to specify when this value is changed we need to refresh all the configuration values so let's specify comma and pass in the value true to the refresh all property in here so anytime a change is seen in this refresh all property it's going to refresh all the values from azure app configuration now let's see this in action so let's run this let's click execute now this returns as expected so if we hover over the configuration we can see the new because i had restarted this application and the value is two so let's resume this execution go back to azure app configuration and make changes to weather.com so let's say this is now back to five click apply and also update the url to be my url so let's remove the new and see if this works now that we have done updating the configuration values let's come to the refresh all property select edit and specify this to b2 so let's click apply let's navigate back to swagger ui now i've made sure that five seconds has passed so let's click execute again now if we look at the configuration values this is now reflecting as five we also have the web api url as my url again with the new removed now this is because i had updated the refresh all property which in turn triggers a refresh of all the configuration values now anytime i need to make a change in any of these configuration values i can do that and then also update the refresh all property which will trigger a configuration refresh of all the keys that's coming from azure app configuration alternatively you can also use event grid subscriptions to automatically refresh your application configuration now azure app configuration can be set up to publish events to even grids and then we can listen to events from there and refresh our application configuration i will put this link in the description below which goes through that scenario coming back to our application when we initially set this up in the program.cs we had used a connection string from the application configuration now this had a sensitive info and we were not quite comfortable with it so let's see how we can fix that if your applications are getting deployed into the azure infrastructure which i assume it will be if you're using azure app configuration then you can use the managed identity feature of azure in these cases azure will automatically manage an identity for you inside azure infrastructure and use that to set up the authentication between resources if you're new to managed identity i highly recommend checking my video linked here to understand more about it to start using managed identity we need to use a new nuget package so let's navigate to the dependencies right click and say manage nuget packages and search for dot identity nuget package so let's install that into the application by clicking the plus button here and clicking install now i have the package installed so let's navigate to the program.cs and start using this now instead of passing in the connection string i will use managed identity to do that let me specify a new object so let's say credential and use new default azure credential which is coming from the azure azure.identity package that we just created now depending on where this application is running the default azure credential class is configured to get a token from different sources now if you're running on your local environment it can be configured to get the values from let's say an environment variable or from your cli login or even from the ide integrations i had done a complete video on how to set up local development environment when you're using default azure credential in this example i had installed an extension to jetbrains rider which sets up integration with azure so if i go into tools i can see azure and i can see the subscriptions that's associated with this so there is a credential that's automatically getting picked up from my azure cli that's already running on my computer i'll not go into the details of this but you can check out the other video if you want to get this working on your local environment as well so let's use the credentials now and instead of passing in a connection string we can use the overload to pass in this credential now this expects a uri parameter as the first value which is the uri of our azure app config which i'll populate in a moment and pass the credential that we have just created now for the uri we can get this from the app configuration so if we go back to appsettings.json we can simply use this url as the uri that we require so let's remove all the other values from this config and let's just have the url to this app configuration let's navigate back to program.cs let's rename this to app uri and use this from our app uri you can also move this away from connection string if you want to but let's leave this like this for now now when i run this application this is going to pick up the credentials from my local environment to do this like i mentioned i have used this azure toolkit for rider plugin so that it can connect with the azure connections that i have i have configured the toolkit to use my cli tokens to connect to azure so if i go into file and go to settings and search for azure i can see under tools i have an azure and then service authentication this is currently configured to use the cli logins you can change this if you want a different method as well this is available when you configure this plugin for visual studio there is a different way to do this which i walked through in my other video since this is using credentials from my cli it will be logging in under my account for my email id so let's go back to azure app configuration and configure my account to have access to this configuration that i've just created to do that let's go into iam which is the access control menu on the left and click add and specify add role assignment here i can select a new role so let's say i want to give an app configuration data reader which allows reading access to the app configuration and also select an email address that i want to assign to so let me assign the value for my account that is used for my cli let's click save which adds the role assignment to this particular app configuration now with this configured let's come back to rider and run this application to see if this is working as expected the application is running successfully so let's navigate to this endpoint and execute it to confirm this is getting all the values so if we hover over the value we can see this has five and all the values retrieved from application configuration we no longer have any sensitive information within our application so whenever the application hits it uses the default azure credential now since this is running on my local environment it uses the cli to get the token and then uses that to connect to app configuration since i configured app configuration to have read access for my id i am able to successfully connect to it and retrieve values from there if we are deploying this application into azure this will work using managed identities within the infrastructure let's see how we can do that so let's right click on azure app configuration click publish and then select azure this is available through the azure toolkit that i have installed if you're using visual studio this is also available from inside there normally to deploy applications to azure i would set up a build deploy pipeline which i have shown you in other videos in this youtube channel so let's click create a new web app let's have this automated web app name select the subscription let's install this into the same resource group that we used and then specify windows and leave everything else as the default let's change the pricing standard to be free because i don't want this to charge any money for me so let's click run which is going to deploy this whole application into azure right from my ide but like i mentioned normally for a real world application i would set up a build deploy pipeline we can see in the console here that it is trying to create the web app package and deploy it into the configurations that we have specified so the deploy is successful which means the web app should be running inside our azure so let's navigate back to the azure portal go into our resource group so let's go to overview and select the resource group that we created we have the web app successfully deployed in here so let's navigate to that and turn on managed identity for this to do that let's scroll on the left and go to the identity section in there and click on and you can click save this is going to enable a system assigned managed identity for this web application now azure is going to manage the identity for this application so you don't have to take care of any sensitive information by your own let's navigate back to our app configuration so let's go into youtube demo which is the resource group and go into the app configuration that we created inside the iam let's go to add and click role assignment again so here we can specify assign access to and specify a system assigned identity and choose app service now we automatically have the web app that we created so we can select that from here let's also give the same data reader access to this web app now in this case what is happening is azure has automatically created an identity and you are assigning that an access to data reader this is the identity of our application which we just turned on using managed identities so let's click save which is going to add that role assignment for this application if you navigate into role assignments here you can see all the role assignments that's available for this app configuration that we have set up let's navigate back into the resource group and to the web app that we had created from the overview tab let's navigate to this url to launch the web app that we have just deployed now if we navigate into rider we can see under startup.cs the swagger endpoints are only configured if it's a development environment since the deployed environment is not development we will have to directly hit the api to see whether it's working or not so let's navigate to slash weather forecast which is the endpoint of the api so here we can see the values coming up as expected now if we navigate back to app configuration so let's go into the youtube demo resource group go to app configuration and change this value for the count which should be reflecting inside our api as well so let's navigate to configuration explorer go to the count property edit this and specify this as 20. so let's click apply let's also not forget to go into refresh all because that is required to refresh the configuration values and make this as 3. let's click apply again so after 5 seconds since the refresh all values has changed the app configuration will be automatically refreshed and we will start getting 20 values inside our api so let's navigate to the web application and refresh the request to the api here now we can see 20 weather data coming back as the response to this api call so all the changes that we have made is automatically reflected it is also using managed identity to authenticate with the app configuration we didn't have to set up any sensitive information or keys for these two components to talk with each other i hope this helps you to understand more about azure app configuration how it works and also how to use this from an asp.net core application the app configuration also provides features to integrate with keyword where you can seamlessly get keys from an azure keyword now the azure app configuration complements the keyword functionality with azure app configuration you are able to seamlessly refresh and reload configurations from your application now just like we had this web app we can have an azure function or a web job all talking to this same app configuration and retrieving the configuration values it provides a single store for all your configuration needs and you don't have to write additional code to automatically reload them as well if you like this video please make sure to hit the like button and also leave comments if you have any question or feedback if you want to be notified of the future such videos please make sure to hit subscribe thank you and see you soon
Info
Channel: Rahul Nath
Views: 2,763
Rating: 5 out of 5
Keywords: azure app configuration, azure app configuration service, azure app configuration from asp.net core, configuration in asp.net core, securely store connection string, how to use azure app configuration, central store for config values, Microsoft.Azure.AppConfiguration.AspNetCore, azure managed identity, azure app configuration managed identity, jetbrains rider azure managed identity, how to securely store configuration in asp net core, asp net core configuration store
Id: VEjYW14WKRM
Channel Id: undefined
Length: 38min 16sec (2296 seconds)
Published: Thu Jul 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.