MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.2k stars 21.34k forks source link

Microsoft.Azure.WebJobs SDK for creating Azure Functions has issues in reading local.settings.json if IConfiguration overrode by custom configuration. #82589

Open v-anvari opened 2 years ago

v-anvari commented 2 years ago

Can this note or warning can be update in the document as appropriate. Initially posted in the Function-Host repo -

Repro steps Step A: Create a function app, Step B: Add Queue Trigger functions, Step C: Add code to use Dependency injection, Step D: Create an empty IConfiguration using -

IConfiguration configuration = new ConfigurationBuilder().Build();
....
builder.Services.AddSingleton<IConfiguration>(configuration);

Step E: After overridden default configuration all functions (Queue, Timer, etc.) expecting Connection strings from local.settings.json or from the environment variables will start failing. A host error has occurred during startup operation '4a5fff7d-95bd-4f2f-933c-eb0f789b3e6c'. [2020-10-10T03:08:59.833] Microsoft.Azure.WebJobs.Extensions.DurableTask: Unable to find an Azure Storage connection string to use for this binding.

Expected behavior After investigation, I found out that all the required settings (cron, connection strings) on which function binding depends are not only driven from environment variables values but these environment variables must be available in Configuration injected or available through DI.

Example:

If I have a Queue trigger with like:

[FunctionName("MyFunction")]
public static void Run([QueueTrigger("myqueue-items", Connection = "MyQueueConnection")]string myQueueItem, ILogger log)
{
...
}

then MyQueueConnection must be available in the Configuration section with the same name. But after injected a configuration excepting environment variables, it starts failing.

Actual behavior This warning or note must be mentioned in the default templates or on documentation as while overriding the Configuration in DI then there should be AddEnvironmentVariables() injected or the required settings must be manually added as configuration sections under IConfiguration.

For this specific e.g.

IConfiguration configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build(); Known workarounds Make sure to check Configuration available in DI must have required binding settings.

Related information Provide any related information

Programming language used: C# Links to source Bindings used: Queue/Timer


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

SwathiDhanwada-MSFT commented 2 years ago

@v-anvari Thanks for your comment. Can you please share the document you were referring to where the changes needs to be done so that I can redirect to appropriate team?

SwathiDhanwada-MSFT commented 2 years ago

Assigning to content author for inputs