-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add guidance on using configuration in Startup #32962
Comments
Thanks for the feedback! We are currently investigating and will update you shortly. |
I would be very interested in the outcome of this. I'm trying to use the Options pattern described in other Microsoft documentation here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2 but am not sure what the advised way of accessing the IConfiguration object. I have previously had issues with creating my own configuration object, as one is registered automatically for use in my functions, but I also cannot inject into the Startup class. |
@benbelow good point, I did also try the options pattern and you can do it but only with an Action where you set each property using GetEnvironmentVariable from individual settings in the Values section (if local) or app settings in Azure. This works but it’s clunky. |
There is no published guidance around this. My Host.json {
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"SubscriptionId": "XXXXXXXXXXXXXXXXXX", I am currently doing public class Startup : FunctionsStartup
{
IConfiguration _configuration;
public override void Configure(IFunctionsHostBuilder builder)
{
_configuration = builder.Services
.Where(s => s.ServiceType == typeof(IConfiguration)).First()
.ImplementationInstance as IConfiguration;
}
} and then var SubscriptionId = _configuration["AzureFunctionsJobHost:SubscriptionId"]; It works, but I don't know if this is the optimal way. |
Looks like we need to wait until #4464 is addressed |
Hi, Just thought to give my input, on how I did this (also supporting complex configs): My local.settings.json:
Startup.cs of the function:
And the OriginationConfiguration.cs respects the "json" structure of the local.settings.json:
You can't have a complex structure in the local.settings.json... but what you do is you respect the convention of delimiting nodes with : , just like Works well on local as well as in Azure. The only thing that I don't like is having to build the ServiceProvider in the StartupClass... adding the IConfiguration to the function's constructor works fine, but that's not where you want to build your service collection (assuming your service collection needs some configs), so I would like to see a more elegant approach and/or how to get the IConfiguration in the Startup. Vlad |
@fabiocav - can you please comment on what's possible here? |
I'm currently using this code: public override void Configure(IFunctionsHostBuilder builder)
{
var configBuilder = new ConfigurationBuilder();
configBuilder.AddEnvironmentVariables();
var configuration = configBuilder.Build();
builder.Services.Configure<FunctionAppOptions>(configuration.GetSection("FunctionAppOptions"));
} |
@StefH Could you please expand your example on how to access the configuration in a function? I'm somewhat at loss here. |
@HSBallina I created a NuGet package that should help. Check it out. |
@HSBallina My full example project can be found at https://github.com/StefH/SmartContractFunction/tree/master/src/SmartContractAzureFunctionApp See the @MisinformedDNA Your solution uses the same principles as I use in my sample project. |
#reassign:craigshoemaker |
Configuration support is a feature that the product team is investigating. Since this is more of a product issue than a doc issue, I am going to #please-close this thread for now. To request specific features, please feel free to open an issue propose them in the azure-functions-host repo. |
Would be good to understand the best practice for using configuration in the Startup (FunctionsStartup) class, seems confusing which options work locally vs in Azure. I've tried:
Would be good to know the preferred approach and if this is still a work in progress, thanks.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: