简体   繁体   中英

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?

I'm trying to make use of Azure App Configuration from within an Azure Function v1 using .NET Framework. I would ideally like to make use of the ConfigurationBuilders .

This would mean using the SimpleJsonConfigBuilder class when running on a dev machine but using AzureAppConfigurationBuilder when hosted in Azure.

I've got it working with our Web API projects but I'm stuck when it comes to the Azure Functions.

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.

<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?

ConfigurationManager.AppSettings is readonly at the point I'm accessing, so I cannot update it myself.

We do have an IAppSettings interface for which I have written an implementation to use the 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.

1.Install pacakge Microsoft.Extensions.Configuration.AzureAppConfiguration

2.Create Function .net in VS and use the code as below:

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: 在此处输入图片说明 4.The package I have used: 在此处输入图片说明

For more details, you could refer to this article (It's also suitable for .net framework).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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