简体   繁体   English

如何在Azure Function v1(.NET Framework)中使用Azure App配置服务

[英]How can I use Azure App Configuration service within Azure Function v1 (.NET Framework)

TL;DR: How can I use Managed Service Identities in an Azure Function v1 for configuring via Azure App Configuration? TL; DR:如何在Azure Function v1中使用托管服务身份通过Azure App Configuration进行配置?

I'm trying to make use of Azure App Configuration from within an Azure Function v1 using .NET Framework. 我正在尝试使用.NET Framework在Azure Function v1中利用Azure App Configuration。 I would ideally like to make use of the ConfigurationBuilders . 理想情况下,我想利用ConfigurationBuilders

This would mean using the SimpleJsonConfigBuilder class when running on a dev machine but using AzureAppConfigurationBuilder when hosted in Azure. 这意味着在开发计算机上运行时使用SimpleJsonConfigBuilder类,而在Azure中托管时使用AzureAppConfigurationBuilder

I've got it working with our Web API projects but I'm stuck when it comes to the Azure Functions. 我已经将其与我们的Web API项目一起使用,但是在谈到Azure函数时却陷入了困境。

The problem as I see it, is that the Azure Function host is using its own web.config and therefore cannot be modified with the config I would use in a web project. 我所看到的问题是,Azure Function主机正在使用其自己的web.config,因此无法使用将在Web项目中使用的配置进行修改。

<configuration>
  <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="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=1.0.0.0, Culture=neutral" />
      <add name="Json" optional="true" mode="Greedy" jsonFile="~/App_Data/settings.json" type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json, Version=1.0.0.0, Culture=neutral" />
      <add name="AzureViaConnectionString" optional="true" mode="Greedy" connectionString="${AppConfigConnectionString}" type="Microsoft.Configuration.ConfigurationBuilders.AzureAppConfigurationBuilder, Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration" />
      <add name="AzureViaMsi" optional="true" mode="Greedy" endPoint="${AppConfigEndpoint}" type="Microsoft.Configuration.ConfigurationBuilders.AzureAppConfigurationBuilder, Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration" />
    </builders>
  </configBuilders>
  <appSettings configBuilders="Environment,Json">
...

Even if I don't use the xml config, which is I'm assuming will not be possible, is there a way to modify the ConfigurationManager.AppSettings as seems to happen with the ConfigBuilders? 即使我不使用XML配置,这是我假设是不可能的,有没有修改的方式ConfigurationManager.AppSettings作为似乎与ConfigBuilders发生?

ConfigurationManager.AppSettings is readonly at the point I'm accessing, so I cannot update it myself. 我正在访问时, ConfigurationManager.AppSettings是只读的,因此我自己无法更新它。

We do have an IAppSettings interface for which I have written an implementation to use the Azure App Configuration REST api. 我们确实有一个IAppSettings接口,为此我编写了一个实现以使用Azure App Configuration REST api。 However, that means storing the secret associated with the service in the usual function app settings, rather than being able to use Managed Service Identities. 但是,这意味着在通常的功能应用程序设置中存储与服务关联的机密,而不是能够使用托管服务标识。

Additionally, the function runtime needs some other settings before it will even reach the point of executing any of my code, meaning that I cannot move all of the settings from the standard location. 此外,函数运行时还需要一些其他设置,才能甚至执行我的任何代码,这意味着我无法从标准位置移走所有设置。

Migrating to Azure Functions V2 is not currently feasible. 迁移到Azure Functions V2当前不可行。

1.Install pacakge Microsoft.Extensions.Configuration.AzureAppConfiguration 1.安装pacakge Microsoft.Extensions.Configuration.AzureAppConfiguration

2.Create Function .net in VS and use the code as below: 2.在VS中创建.net函数,并使用如下代码:

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");
    var builder = new ConfigurationBuilder();
    builder.AddAzureAppConfiguration(Environment.GetEnvironmentVariable("ConnectionString"));
    var config = builder.Build();
    string message = config["TestApp:Settings:Messagejoey"];
    return message == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + message);
}

3.The result snapshot: 3.结果快照: 在此处输入图片说明 4.The package I have used: 4.我使用过的软件包: 在此处输入图片说明

For more details, you could refer to this article (It's also suitable for .net framework). 有关更多详细信息,请参阅本文 (它也适用于.net框架)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM