Has there been a change to the way the IOptions pattern is utilized in a .NET 8 Azure Functions in an Isolated Worker Process? I'm using the following code, and the dependency-injected IOptions object is always null.
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"Example:ClientId": "coke",
"Example:ClientSecret": "g5419c734249b7ed86"
}
}
Program.cs
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices((hostContext, services) =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddOptions<Example>().Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection("Example").Bind(settings);
});
})
.Build();
Options Class
public class Example
{
public string ClientId { get; set; } = string.Empty;
public string ClientSecret { get; set; } = string.Empty;
}
DI Constructor
public class MyClass(
ILogger<MyClass> logger,
IOptions<Example> exampleOptions)
{}
The issue is related to the use of a primary constructor. When I refactored the primary constructor:
To a traditional constructor:
The DI issue was resolved; however, all of the documentation I've read regarding primary constructors indicates DI of IOptions is supported.