How to use IOptions pattern for configuration in dotnet-isolated (net5.0) azure functions

Viewed 644

I'm attempting to port an existing Functions app from core3.1 v3 to net5.0 I but can't figure out how to get the IOptions configuration pattern to work.

The configuration in my local.settings.json is present in the configuration data, and I can get to it using GetEnvironmentVariable. Still, the following does not bind the values to the IOptions configuration like it used to.

.Services.AddOptions<GraphApiOptions>()
    .Configure<IConfiguration>((settings, configuration) => configuration.GetSection("GraphApi").Bind(settings))

The values are in the local.settings.json just as they were before:

    "GraphApi:AuthenticationEndPoint": "https://login.microsoftonline.com/",
    "GraphApi:ClientId": "316f9726-0ec9-4ca5-8d04-f39966bebfe1",
    "GraphApi:ClientSecret": "VJ7qbWF-ek_Amb_e747nXW-fMOX-~6b8Y6",
    "GraphApi:EndPoint": "https://graph.microsoft.com/",
    "GraphApi:TenantId": "NuLicense.onmicrosoft.com",

Is this still supported? What am I missing?

1 Answers

I had the same issue, but turns out that the json was not correctly formatted.

Just for reference, here it is how I configured it:

var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices(s =>
            {
                s.AddOptions<ApplicationSettings>().Configure<IConfiguration>((settings, configuration) =>
                {
                    configuration.GetSection(nameof(ApplicationSettings)).Bind(settings);
                });
            })
            .Build();

And here is an example of local.setting.json:

{
  "IsEncrypted": false,
  "Values": {
    "ApplicationSettings:test": "testtest",
    "ApplicationSettings:testtest": "test"
  }
}
Related