Azure / azure-functions-dotnet-worker-preview

MIT License
65 stars 19 forks source link

How can I bind options #47

Closed josere closed 3 years ago

josere commented 3 years ago

It used to work that I created a MyOptions csharp object and then call during configuration:

serviceCollection.AddOptions<MyOptions>()
                        .Configure<IConfiguration, ILogger<MyOptions>>((d, c, l) => c.GetSection("Options").Bind(d));

my function then can have a (IOptions config) parameter, with the Value set to a object populated with values from the settings where I can set "Options__Name": "Value1".

so config.Value.Name was set to "Value1". This doesn't seem to work anymore. Basically, configuration.GetSection("Options").Exists() returns false.

I can see that Environment.GetEnvironmentVariable("Options__Name") has the "Value1" value.

It would be nice if the sample app have a sample of user config values.

Thanks, Jose.

josere commented 3 years ago

Well, i made it work with this changes:

  1. Use ":" notation in local.settings.json: "Options:Name": "Value1"
  2. Change the host builder creation:

var host = Host.CreateDefaultBuilder(args) .ConfigureFunctionsWorker((_, b) => { b.UseFunctionExecutionMiddleware(); }) .ConfigureServices((c, s) => {

  3. Bind like this:
               s.AddOptions<MyOptions>()
                    .Bind(c.Configuration.GetSection("Options"));