Tutorial: Use dynamic configuration in an Azure Functions does not work in NET 8.0 because appUseAzureAppConfiguration

221 views Asked by At

I am try to made to work the example in Microsoft: Tutorial: Use dynamic configuration in an Azure Functions app but as original actually does not work for me, the functions call the Timer function, but stop there. I discovered that the reason was this line of code:

.ConfigureFunctionsWorkerDefaults(app =>
{
    // Use Azure App Configuration middleware for data refresh.
    app.UseAzureAppConfiguration();
})

If you eliminate the line the application work, then my question is: If I eliminate the code the middleware continue to works or not.

I am using NET 8.0, maybe this change here. In the other hand I use the same code than the tutorial.

1

There are 1 answers

0
Ikhtesam Afrin On BEST ANSWER

I have followed this MS Doc too and used .NET8 to get the value from App Configuration. Below code and configuration worked for me.

ShowMessage.cs-

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Net;

namespace _77963346
{
    public class ShowMessage
    {
        private readonly IConfiguration _configuration;
        private readonly ILogger<ShowMessage> _logger;

        public ShowMessage(IConfiguration configuration, ILogger<ShowMessage> logger)
        {
            _configuration = configuration;
            _logger = logger;
        }

        [Function("ShowMessage")]
        public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");
            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            // Read configuration data
            string key = "TestApp:Settings:Message";
            string message = _configuration[key];

            await response.WriteStringAsync(message ?? $"Please create a key-value with the key '{key}' in Azure App Configuration.");

            return response;
        }
    }
}

Program.cs-

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureAppConfiguration(builder => 
    {
        builder.AddAzureAppConfiguration(options =>
        {
            options.Connect(Environment.GetEnvironmentVariable("ConnectionString"))
                    // Load all keys that start with `TestApp:` and have no label
                    .Select("TestApp:*")
                    .ConfigureRefresh(refreshOptions =>
                        refreshOptions.Register("TestApp:Settings:Message", refreshAll: true));
        });
    })
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
        services.AddAzureAppConfiguration();
    })
    .ConfigureFunctionsWebApplication(app => 
    {
        app.UseAzureAppConfiguration();
    })
    .Build();

host.Run();

.csproj-

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <RootNamespace>_77963346</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Azure.AppConfiguration.Functions.Worker" Version="7.1.0-preview" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

Using the given code, I am able to fetch the value from App Configuration as shown below-

enter image description here

enter image description here