Eli Pulsifer
Eli Pulsifer

Reputation: 763

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

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?

Upvotes: 5

Views: 2236

Answers (2)

Rajib Mahata
Rajib Mahata

Reputation: 33

I have updated environment variables from GetEnvironmentVariable to IOptions. Following steps I follows.

  1. Include nuget packages
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="6.0.1" />
  1. Created a start up file and configure AddAzureAppConfiguration and AddOptions. "AppConfigVariables" is my class where I have declared my environment variables.
[assembly: FunctionsStartup(typeof(MyApp.Startup))]
namespace MyApp
{
    public class Startup : FunctionsStartup
    {
        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {
            // Method intentionally left empty.
        }

        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddAzureAppConfiguration();
            builder.Services.AddOptions<AppConfigVariables>()
                             .Configure<IConfiguration>((settings, configuration) =>
                             {
                                 configuration.Bind(settings);
                             });
        
        }
    }
}
  1. In Constructor you can access environment variables by invoking ioptions.
public class GetAllData
{
    private readonly AppConfigVariables appConfigVariables;
    
    public GetAllData(IOptions<AppConfigVariables> options)
    {
        appConfigVariables = options.Value;
        
    }

Let me know if it won't work for you.

Upvotes: 0

hristo.marinov
hristo.marinov

Reputation: 21

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"
  }
}

Upvotes: 2

Related Questions