Skip to content

Files

Latest commit

4a4ceff · Apr 5, 2022

History

History
148 lines (97 loc) · 5.29 KB

quickstart-dotnet-core-app.md

File metadata and controls

148 lines (97 loc) · 5.29 KB
title description services author ms.service ms.devlang ms.topic ms.custom ms.date ms.author
Quickstart for Azure App Configuration with .NET Core | Microsoft Docs
In this quickstart, create a .NET Core 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
quickstart
devx-track-csharp, mode-other
04/05/2022
malev

Quickstart: Create a .NET Core app with App Configuration

In this quickstart, you incorporate Azure App Configuration into a .NET Core console app to centralize storage and management of application settings separate from your code.

Prerequisites

Create an App Configuration store

[!INCLUDE azure-app-configuration-create]

  1. Select Configuration Explorer > Create > Key-value to add the following key-value pairs:

    Key Value
    TestApp:Settings:Message Data from Azure App Configuration

    Leave Label and Content Type empty for now.

  2. Select Apply.

Create a .NET Core console app

You use the .NET Core command-line interface (CLI) to create a new .NET Core console app project. The advantage of using the .NET Core CLI over Visual Studio is that it's available across the Windows, macOS, and Linux platforms. Alternatively, use the preinstalled tools available in the Azure Cloud Shell.

  1. Create a new folder for your project.

  2. In the new folder, run the following command to create a new .NET Core console app project:

    dotnet new console
    

Connect to an App Configuration store

  1. Add a reference to the Microsoft.Extensions.Configuration.AzureAppConfiguration NuGet package by running the following command:

    dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
    
  2. Run the following command to restore packages for your project:

    dotnet restore
    
  3. Open Program.cs, and add a reference to the .NET Core App Configuration provider.

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Configuration.AzureAppConfiguration;
  4. Update the Main method to use App Configuration by calling the builder.AddAzureAppConfiguration() method.

    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder();
        builder.AddAzureAppConfiguration(Environment.GetEnvironmentVariable("ConnectionString"));
    
        var config = builder.Build();
        Console.WriteLine(config["TestApp:Settings:Message"] ?? "Hello world!");
    }

Build and run the app locally

  1. Set an environment variable named ConnectionString, and set it to the access key to your App Configuration store. At the command line, run the following command:

    To build and run the app locally using the Windows command prompt, run the following command:

    setx ConnectionString "connection-string-of-your-app-configuration-store"

    Restart the command prompt to allow the change to take effect. Print the value of the environment variable to validate that it is set properly.

    If you use Windows PowerShell, run the following command:

    $Env:ConnectionString = "connection-string-of-your-app-configuration-store"
    

    If you use macOS, run the following command:

    export ConnectionString='connection-string-of-your-app-configuration-store'

    Restart the command prompt to allow the change to take effect. Print the value of the environment variable to validate that it is set properly.

    If you use Linux, run the following command:

    export ConnectionString='connection-string-of-your-app-configuration-store'

    Restart the command prompt to allow the change to take effect. Print the value of the environment variable to validate that it is set properly.


  2. Run the following command to build the console app:

    dotnet build
    
  3. After the build successfully completes, run the following command to run the app locally:

    dotnet run
    

Clean up resources

[!INCLUDE azure-app-configuration-cleanup]

Next steps

In this quickstart, you created a new App Configuration store and used it with a .NET Core console app via the App Configuration provider. To learn how to configure your .NET Core app to dynamically refresh configuration settings, continue to the next tutorial.

[!div class="nextstepaction"] Enable dynamic configuration