AzureFunction.Isolated.HostConfigurator 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AzureFunction.Isolated.HostConfigurator --version 1.0.0
NuGet\Install-Package AzureFunction.Isolated.HostConfigurator -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AzureFunction.Isolated.HostConfigurator" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AzureFunction.Isolated.HostConfigurator --version 1.0.0
#r "nuget: AzureFunction.Isolated.HostConfigurator, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AzureFunction.Isolated.HostConfigurator as a Cake Addin
#addin nuget:?package=AzureFunction.Isolated.HostConfigurator&version=1.0.0

// Install AzureFunction.Isolated.HostConfigurator as a Cake Tool
#tool nuget:?package=AzureFunction.Isolated.HostConfigurator&version=1.0.0

Problem

The new dotnet-isolated hosting model of Azure Functions doesn't allow you to configure the connection for triggers or binding the same way it does in the In-Process model. This is a problem when you want to use a connection string from Key Vault, for example. The problem is that the host process needs the connection before it calls into your code (i.e. before your Program.cs is called) so you only have a couple of ways to configure it:

  • Use an environment variable
  • Use a local.settings.json file

Both of these options may not be sufficient to cover your scenario so this package helps you solve that.

How it works

Azure Functions using the dotnet-isolated hosting model will load the extensions that your project references and will initialize the extensions before trying to resolve the connection strings.

This step happens inside the AddScriptHost method of the ScriptHostBuilderExtensions when the host calls the HasExternalConfigurationStartups() method. (the code I'm talking about here is in the azure-function-host project on GitHub here.

So this package essentially hooks into that functionality to allow you to customize the host configuration before it's used'.

This package implements a WebJob extension that will delegate the configuration to a type that you specify in your project using the assembly attribute [HostConfiguratorAttribute(typeof(TheTypeToBeInvoked))]. You need to specify a type that implements the interface IWebJobsConfigurationStartup from the package Microsoft.Azure.WebJobs in the attribute, this type will then be invoked by this extension allowing you to add additional configuration sources to the host configuration.

If you are targeting .net 7.0, you can use the generic version of the attribute [HostConfiguratorAttribute<T>] which will allow you to specify the type of the configurator directly in the attribute.

Usage

  1. Add the package AzureFunctions.Extensions.Configuration to your project.
  2. Implement the interface IWebJobsConfigurationStartup from the package Microsoft.Azure.WebJobs in your project.
  3. Add the attribute [HostConfiguratorAttribute(typeof(TheTypeToBeInvoked))] to your project.

Caveats

If you need to call AddJsonFile('appsettings.json'), make sure to call Path.Combine(context.ApplicationRootPath, "appsettings.json") because the host will not be running from the same directory your code is running from.

For now, your assembly will be loaded twice, once in the host process and once by the Azure Functions host, so if you have any static initialization code, it may potentially be called twice. This is a known issue and will be fixed in the future potentially using the AssemblyLoadContext functionality. A simple workaround may be to define a standalone assembly that contains the configuration code instead of adding the type in your functions code directly.

Samples

Check the /samples folder for a sample project.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 215 8/18/2023
1.0.1 139 7/31/2023
1.0.0 144 7/28/2023