title | description | services | author | ms.service | ms.devlang | ms.custom | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|
Quickstart for Azure App Configuration with .NET Framework |
In this article, create a .NET Framework app with Azure App Configuration to centralize storage and management of application settings separate from your code. |
azure-app-configuration |
maud-lv |
azure-app-configuration |
csharp |
devx-track-csharp, mode-other, devx-track-dotnet |
quickstart |
05/24/2024 |
malev |
There are two ways to incorporate Azure App Configuration into a .NET Framework-based app.
- The configuration builder for App Configuration enables data from App Configuration to be loaded to App Settings. Your app accesses configuration as it always does via
ConfigurationManager
. You don't need to make any code change other than updates to app.config or web.config files. This quickstart will walk you through this option. - As is designed by the .NET Framework, the App Settings can only refresh upon application restart. The App Configuration .NET provider is a .NET Standard library. It supports caching and refreshing configuration dynamically without application restart. If the dynamic configuration is essential to you and you are willing to make code changes, see tutorials on how you can implement dynamic configuration updates in a .NET Framework console app or an ASP.NET web app.
In this quickstart, a .NET Framework console app is used as an example, but the same technique applies to an ASP.NET Web Forms/MVC app.
- An Azure account with an active subscription. Create one for free.
- An App Configuration store. Create a store.
- Visual Studio
- .NET Framework 4.7.2 or later
Add the following key-value to the App Configuration store and leave Label and Content Type with their default values. For more information about how to add key-values to a store using the Azure portal or the CLI, go to Create a key-value.
Key | Value |
---|---|
TestApp:Settings:Message | Data from Azure App Configuration |
-
Start Visual Studio and select Create a new project.
-
In Create a new project, filter on the Console project type and select Console App (.NET Framework) with C# from the project template list. Press Next.
-
In Configure your new project, enter a project name. Under Framework, select .NET Framework 4.7.2 or higher. Press Create.
-
Right-click your project, and select Manage NuGet Packages. On the Browse tab, search and add the following NuGet packages to your project.
- Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration version 1.0.0 or later
- Microsoft.Configuration.ConfigurationBuilders.Environment version 2.0.0 or later
- System.Configuration.ConfigurationManager version 4.6.0 or later
-
Update the App.config file of your project as follows:
<configSections> <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" /> </configSections> <configBuilders> <builders> <add name="MyConfigStore" mode="Greedy" connectionString="${ConnectionString}" type="Microsoft.Configuration.ConfigurationBuilders.AzureAppConfigurationBuilder, Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration" /> <add name="Environment" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" /> </builders> </configBuilders> <appSettings configBuilders="Environment,MyConfigStore"> <add key="AppName" value="Console App Demo" /> <add key="ConnectionString" value ="Set via an environment variable - for example, dev, test, staging, or production connection string." /> </appSettings>
The connection string of your App Configuration store is read from the environment variable
ConnectionString
. Add theEnvironment
configuration builder before theMyConfigStore
in theconfigBuilders
property of theappSettings
section. -
Open Program.cs, and update the
Main
method to use App Configuration by callingConfigurationManager
.static void Main(string[] args) { string message = System.Configuration.ConfigurationManager.AppSettings["TestApp:Settings:Message"]; Console.WriteLine(message); Console.ReadKey(); }
-
Set an environment variable named ConnectionString to the read-only key connection string obtained during your App Configuration store creation.
If you use the Windows command prompt, run the following command:
setx ConnectionString "connection-string-of-your-app-configuration-store"
If you use Windows PowerShell, run the following command:
$Env:ConnectionString = "connection-string-of-your-app-configuration-store"
-
Restart Visual Studio to allow the change to take effect.
-
Press Ctrl + F5 to build and run the console app. You should see the message from App Configuration outputs in the console.
[!INCLUDE azure-app-configuration-cleanup]
In this quickstart, you created a new App Configuration store and used it with a .NET Framework console app. To learn how to enable your .NET Framework app to dynamically refresh configuration settings, continue to the next tutorials.
[!div class="nextstepaction"] Enable dynamic configuration in a .NET Framework app
[!div class="nextstepaction"] Enable dynamic configuration in an ASP.NET web app