快速入门:使用 Azure 应用配置创建 .NET Framework 应用Quickstart: Create a .NET Framework app with Azure App Configuration

在本快速入门中,会将 Azure 应用程序配置合并到基于 .NET Framework 的控制台应用中,以集中存储和管理与代码分离的应用程序设置。In this quickstart, you incorporate Azure App Configuration into a .NET Framework-based console app to centralize storage and management of application settings separate from your code.

先决条件Prerequisites

创建应用配置存储区Create an App Configuration store

  1. 若要创建新应用程序配置存储区,请登录 Azure 门户To create a new App Configuration store, sign in to the Azure portal. 在主页的左上角,选择“创建资源” 。In the upper-left corner of the home page, select Create a resource. 在“搜索市场”框中,输入“应用程序配置”并选择 Enter 。In the Search the Marketplace box, enter App Configuration and select Enter.

    搜索应用配置

  2. 在搜索结果中选择“应用程序配置”,然后选择“创建” 。Select App Configuration from the search results, and then select Create.

    选择“创建”

  3. 在“创建应用配置”窗格中,输入以下设置:On the Create App Configuration pane, enter the following settings:

    设置Setting 建议的值Suggested value 说明Description
    订阅Subscription 订阅Your subscription 选择要用来测试应用配置的 Azure 订阅。Select the Azure subscription that you want to use to test App Configuration. 如果帐户只有一个订阅,则会自动选择该订阅并且不显示“订阅”列表 。If your account has only one subscription, it's automatically selected and the Subscription list isn't displayed.
    资源组Resource group AppConfigTestResources AppConfigTestResources 为应用程序配置存储区资源选择或创建资源组。Select or create a resource group for your App Configuration store resource. 此组可用于组织多个资源,删除该资源组可以同时删除这些资源。This group is useful for organizing multiple resources that you might want to delete at the same time by deleting the resource group. 有关详细信息,请参阅使用资源组管理 Azure 资源For more information, see Use resource groups to manage your Azure resources.
    资源名称Resource name 全局唯一名称Globally unique name 输入要用于应用程序配置存储区资源的唯一资源名称。Enter a unique resource name to use for the App Configuration store resource. 该名称必须是包含 5 到 50 个字符的字符串,只能包含数字、字母和 - 字符。The name must be a string between 5 and 50 characters and contain only numbers, letters, and the - character. 该名称的开头或末尾不能是 - 字符。The name can't start or end with the - character.
    位置Location 中国东部 2China East 2 使用“位置”指定在其中托管应用配置存储区的地理位置 。Use Location to specify the geographic location in which your app configuration store is hosted. 为获得最佳性能,请在应用程序的其他组件所在的同一区域创建资源。For the best performance, create the resource in the same region as other components of your application.
    定价层Pricing tier 免费Free 选择所需的定价层。Select the desired pricing tier. 有关详细信息,请参阅应用配置定价页For more information, see the App Configuration pricing page.
  4. 选择“查看 + 创建”以验证自己的设置 。Select Review + create to validate your settings.

  5. 选择“创建”。Select Create. 部署可能需要几分钟。The deployment might take a few minutes.

  6. 部署完成后,导航到“应用程序配置”资源。After the deployment finishes, navigate to the App Configuration resource. 选择“设置” > “访问密钥”。Select Settings > Access keys. 记下只读主密钥连接字符串。Make a note of the primary read-only key connection string. 稍后将使用此连接字符串来配置应用程序,以与创建的应用程序配置存储区进行通信。You'll use this connection string later to configure your application to communicate with the App Configuration store that you created.

  1. 选择“配置资源管理器” > “创建” > “键-值”来添加以下键值对 :Select Configuration Explorer > Create > Key-value to add the following key-value pairs:

    密钥Key Value
    TestApp:Settings:MessageTestApp:Settings:Message Azure 应用配置的数据Data from Azure App Configuration

    暂时将“标签”和“内容类型”保留为空 。Leave Label and Content Type empty for now.

  2. 选择“应用”。 Select Apply.

创建 .NET 控制台应用Create a .NET console app

  1. 启动 Visual Studio,并选择“文件” > “新建” > “项目” 。Start Visual Studio, and select File > New > Project.

  2. 创建新项目 中,针对“控制台” 项目类型进行筛选,然后单击“控制台应用(.NET Framework)” 。In Create a new project, filter on the Console project type and click on Console App (.NET Framework). 选择“下一页”。Select Next.

  3. 配置新项目 中,输入项目名称。In Configure your new project, enter a project name. 在“框架” 下,选择“.NET Framework 4.7.1” 或更高版本。Under Framework, select .NET Framework 4.7.1 or higher. 选择“创建” 。Select Create.

连接到应用程序配置存储区Connect to an App Configuration store

  1. 右键单击项目,然后选择“管理 NuGet 包” 。Right-click your project, and select Manage NuGet Packages. 在“浏览”选项卡中,搜索以下 NuGet 包并将其添加到项目中 。On the Browse tab, search and add the following NuGet packages to your project.

    Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration 1.0.0 or later
    Microsoft.Configuration.ConfigurationBuilders.Environment 2.0.0 or later
    System.Configuration.ConfigurationManager version 4.6.0 or later
    
  2. 如下所述,更新项目的 App.config 文件 :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>
    

    从环境变量 ConnectionString 中读取应用程序配置存储区的连接字符串。The connection string of your App Configuration store is read from the environment variable ConnectionString. appSettings 部分的 configBuilders 属性中,在 MyConfigStore 之前添加 Environment 配置生成器。Add the Environment configuration builder before the MyConfigStore in the configBuilders property of the appSettings section.

  3. 打开 Program.cs 并更新 Main 方法,以通过调用 ConfigurationManager 来使用应用程序配置 。Open Program.cs, and update the Main method to use App Configuration by calling ConfigurationManager.

    static void Main(string[] args)
    {
        string message = System.Configuration.ConfigurationManager.AppSettings["TestApp:Settings:Message"];
    
        Console.WriteLine(message);
        Console.ReadKey();
    }
    

在本地生成并运行应用Build and run the app locally

  1. 通过以下方式更新 App.config文件:将 ${ConnectionString} 替换为应用程序配置实例的实际连接字符串。Update the App.config file by replacing ${ConnectionString} with the actual connection string to your Application Configuration instance. 可以在 Azure 门户中应用程序配置资源的“访问密钥”选项卡中找到该字符串。You can find it in the Access keys tab of the Application Configuration resource in the Azure Portal.

  2. 按 Ctrl+F5 生成并运行控制台应用。Press Ctrl + F5 to build and run the console app.

清理资源Clean up resources

如果不想继续使用本文中创建的资源,请删除此处创建的资源组以避免产生费用。If you do not want to continue using the resources created in this article, delete the resource group you created here to avoid charges.

重要

删除资源组的操作不可逆。Deleting a resource group is irreversible. 将永久删除资源组以及其中的所有资源。The resource group and all the resources in it are permanently deleted. 请确保不要意外删除错误的资源组或资源。Make sure that you don't accidentally delete the wrong resource group or resources. 如果在包含要保留的其他资源的资源组中创建了本文的资源,请从相应的窗格中单独删除每个资源,而不是删除该资源组。If you created the resources for this article inside a resource group that contains other resources you want to keep, delete each resource individually from its respective pane instead of deleting the resource group.

  1. 登录到 Azure 门户,然后选择“资源组”。Sign in to the Azure portal, and select Resource groups.
  2. 在“按名称筛选”框中,输入资源组的名称。In the Filter by name box, enter the name of your resource group.
  3. 在结果列表中,选择资源组名称以查看概述。In the result list, select the resource group name to see an overview.
  4. 选择“删除资源组”。Select Delete resource group.
  5. 系统会要求确认是否删除资源组。You're asked to confirm the deletion of the resource group. 重新键入资源组的名称进行确认,然后选择“删除” 。Enter the name of your resource group to confirm, and select Delete.

片刻之后,将会删除该资源组及其所有资源。After a few moments, the resource group and all its resources are deleted.

后续步骤Next steps

在本快速入门中,你已经创建了一个新的应用程序配置存储区,并将其用于 .NET Framework 控制台应用。In this quickstart, you created a new App Configuration store and used it with a .NET Framework console app. 在应用程序启动后,ConfigurationManager 的值 AppSettings 不会更改。The value AppSettings of ConfigurationManager won't change after the application is started. 应用程序配置 .NET Standard 配置提供程序库,但也可在 .NET Framework 应用中使用。The App Configuration .NET Standard configuration provider library, however can also be used in a .NET Framework app. 若要了解如何启用 .NET Framework 应用以动态刷新配置设置,请继续学习下一个教程。To learn how to enable your .NET Framework app to dynamically refresh configuration settings, continue to the next tutorial.