Cannot setup configuration in a Isolated Azure Function - .NET 7

1.7k views Asked by At

I am upgrading my function from .NET Core 3.1 up to .NET 7 Isolated

My Function App inherits from a base class which does all my setup that is relevant to all function app. This works perfectly

However, in .NET 7 Isolated, it appears as though function startup is not supported/recommended?

I can create a function initializer class which I can then call to setup my services, this is fine

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults((IFunctionsWorkerApplicationBuilder builder) =>
    {
        
    })
    .ConfigureServices((context, s) =>
    {
           var initializer = new FunctionAppInitializer(s);
           initializer.Run();
    })
    .Build();

    host.Run();

However, I have a problem with configuration as this is not available.

How can I run the method below?

 public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        var kvEndpoint =
            Environment.GetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUP__KEYVAULT__CONFIGURATIONVAULT");
        var environmentName =
            Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        builder.ConfigurationBuilder
            .AddAzureKeyVault(new Uri(kvEndpoint!), new DefaultAzureCredential())
            .SetBasePath(Environment.CurrentDirectory)
            .AddJsonFile("local.settings.json", optional: true)
            .AddJsonFile($"local.settings.{environmentName}.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
    }

This is overriding FunctionStartup.ConfigureAppConfiguration which gives me access to the builder.

With the new method, although I can get to builder inside ConfigureFunctionsWorkerDefaults, the builder object does not have ConfigurationBuilder

Paul

1

There are 1 answers

0
Karpik On

You can use ConfigureHostConfiguration method

var host = new HostBuilder()
.ConfigureHostConfiguration(configHost =>
{
    configHost.AddEnvironmentVariables(prefix: "DOTNET_");
    configHost.AddCommandLine(args);
    configHost.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
})