Please provide example on how to configure IOptions<>, or if no longer available, how to map custom option types to configuration settings, as IConfiguration is no longer exposed, nor injected in Startup class.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Hi @arkiaconsulting Thank you for your feedback! We will review and update as appropriate.
@arkiaconsulting @mike-urnun-msft
for now I am configuring my options like this
builder.Services.AddOptions<InfobloxSettings>()
.Configure<IConfiguration>((settings, configuration) => { configuration.Bind(settings); });
@fabiocav any thoughts here ?
This is a valid request. We should enhance the documentation to provide guidance on how to accomplish this.
/cc @jeffhollan
Thanks @fabiocav . I have assigned this to content author @ggailey777 to update the doc as appropriate.
@fabiocav let me know who or what sample we can use to document? Is the sample above adequate? I assume we'll need some explanation as well myself and @ggailey777 would need
Is Options validation supported in Azure Functions? I have tried the approach described in https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2#options-validation; and while the IOption
is correctly configured (from local.settings.json), the validation is never invoked - either with a custom validation lambda or with the .ValidateDataAnnotations()
approach.
I would still be needing this documentation, I was able to inject whole IConfiguration to my service but Options would be far better approach.
I also tried the example above but the settings object seems to be null every time, where is the actual setting supposed to be, I tried many different ways of putting it in local.settings.json but no luck yet.
Big thanks for any possible help.
@featuresnap it would be interesting to look at your code.
@janitorr options setup and dependency should work. Can you perhaps open an issue at https://github.com/azure/azure-functions-host with a repro and more details on what you're trying to do so we can take a closer look.
One thing I've mentioned in a couple of other related issues is that adding configuration from a source outside of what is supported by Functions could lead to issues with activation and scaling. You need to be careful to make sure the configuration you use there is not what you expect to use for host, triggers or binding configuration. That's one of the key reasons why a configure hook was not exposed.
@fabiocav Thanks for quick answer, I´m not sure if there actually is an issue with IoC or if I just don´t understand how it works from the current docs.
I added a simple test project where I have two function triggers, one gets configuration from IConfiguration and the other is my attempt to use AddOptions mentioned above.
https://github.com/janitorr/AzureFunctionConfigurationTest
In the end it does not matter to me if the given setting is an object or a string, I´d just like to test my logic with unit tests without reference to underlying configuration provider.
edit: fix typo
@fabiocav Thanks for quick answer, I´m not sure if there actually is an issue with IoC or if I just don´t understand how it works from the current docs.
I added a simple test project where I have two function triggers, one gets configuration from IConfiguration and the other is my attempt to use AddOptions mentioned above.
https://github.com/janitorr/AzureFunctionConfigurationTest
In the end it does not matter to me if the given setting is an object or a string, I´d just like to test my logic with unit tests without reference to underlying configuration provider.
edit: fix typo
https://github.com/janitorr/AzureFunctionConfigurationTest/pull/2
So it seems that the binder actually just picks the first "seems legit" value from the local.settings.json
The doc is now updated with a section on working with options and settings.
Thanks to @rameshjanjyam for the code snippet that we used to update the doc with 😉
@featuresnap, were you able to resolve your issue? Please share your results!
I am using azure functions v3 with options validation with data annotations as described here
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1#options-validation
However, validation never gets invoked.
When I manually resolve
var validators = serviceProvider.GetServices<IValidateOptions<MyConfigClass>>();
I do get a validator instance, then, when invoked really does check data annotations.
But it seems it is not injected properly into OptionsFactory<TOptions>
The reason seems to be that WebJobsOptionsFactory
creates the OptionsFactory
by using the constructor that explicitly passes the validations parameter as null, instead of resolving this from container.
public WebJobsOptionsFactory(IEnumerable<IConfigureOptions<TOptions>> setups, IEnumerable<IPostConfigureOptions<TOptions>> postConfigures,
IOptionsLoggingSource logSource, IOptionsFormatter<TOptions> optionsFormatter)
{
_innerFactory = new OptionsFactory<TOptions>(setups, postConfigures);
_logSource = logSource;
// This allows us to wrap behavior around an existing type. It will be null for types we don't log.
_optionsFormatter = optionsFormatter;
}
public OptionsFactory(IEnumerable<IConfigureOptions<TOptions>> setups, IEnumerable<IPostConfigureOptions<TOptions>> postConfigures) : this(setups, postConfigures, validations: null)
{ }
What am I doing wrong?
@craigshoemaker, could you please advise?
Thank you!
@oleh-zheleznyak, I'm experiencing the same, I suspect this is a bug, as I don't see any comments etc that this is intentional.
Most helpful comment
This is a valid request. We should enhance the documentation to provide guidance on how to accomplish this.
/cc @jeffhollan