How to use Azure App Configuration for your ASP.NET Core app | Azure Tips and Tricks


>>Learn how to use the Azure App Configuration
service with ASP.NET Core, in this episode of
Azure Tips and Tricks. Azure app configuration,
can store secrets and settings for you
in a central place, so that your application doesn’t have to store them in source code. In this video, we’ll
take a look at how you can use Azure App Configuration with an ASP.NET Core application and how you
can use feature flags. We’ll start by creating an Azure App Configuration
in the Azure Portal. Let’s add a new resource, and I’ll search for app
configuration like this. There it is. Let’s create one. All right. I have to fill in a name. Let’s call it tipsandtricksconfig
and for next, I’ll select my existing
resource group, and that’s it. Let’s create it. Here it goes. I’ll skip
to when it’s done. This is the app configuration, let’s add some items. First, we’ll go to
the feature Manager. Here, we can add feature flags. We can use these to show and hide
features of our application, I’ll add a new one called “Beta”. Okay. By default it is disabled and let’s add a setting
here and create a new one. I’ll call this MyTextValue
and I’ll type something here. We can use this and the feature flag
in our application. To connect to app configuration, we need a connection string. This one here. Let’s copy
that for later. All right. Here we are in Visual Studio, I have already created
an ASP.NET Core application that can talk
to app configuration. Let’s go through it to
see what I’ve done. First, I’ve added
these NuGet packages to talk to App Configuration
and use feature flags. Next, I’ve changed
this method in program.cs. It will now add the app
configuration when it starts up. To do that, it needs to get
disconnection string, let’s add that. We can do that using
the secret Manager tool. Here I can paste it in
the connection string and it is kept out of my source
code. What else? Here in startup.cs, I’ve added this line to be able to
work with Feature Flags. Also, I’ve added this file which has the feature flag
called Beta in it, and I’ve added a Beta Controller. This will serve up
the index view when the beta Feature Flag is enabled, and that index view is here in
the beta folder under views. To work with feature flags in views, I’ve added this tag helper
in the view imports file, and I use that here
in the layout file. Here in the navigation bar, I’ve added a new menu item. This will only be
visible when the beta Feature Flag is
enabled, simple, right? Finally, I used
the setting that we’ve added to app configuration
in the home index view. Here, I render whatever
the value of that setting is. Let’s build it to see if it works, and here we go. We see the text of the MyTextValue setting which is this
one, so that’s good. We don’t see the beta menu item because the Feature Flag
is disabled by default. Let’s change that. Here is the
feature and enable it. All right. I need to restart
the application because the app configuration settings are
loaded when the app starts up. In a production scenario, it is best to schedule the restart of your app in a period when
you have low traffic, and here we go. This is the beta menu item and
it takes me to the beta page. So app configuration helped
me to keep my settings out of source code and provides
a central place to manage them. It also enables me to
use Feature flags, which can be really
powerful. Go check it out.

Leave a Reply

Your email address will not be published. Required fields are marked *