Azure / Azure/azure-functions-host

Analyzer: Suggest Using IFunctionsConfigurationBuilder

Open
#7,417 2 comments 0 reactions 0 assignees View on GitHub
design Needs: Triage (Functions) supportability supportability: analyzer
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

**Conditions to Trigger Analyzer**
When in FunctionsStartup Configure() method, if ConfigurationBuilder().AddJsonFile() is called..

```csharp
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System.Net.Http;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace TestManageConnectionApp
{


public class Startup : FunctionsStartup
{
private static IConfiguration _configuration;
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
var localEnvironment = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var workingDirectory = !string.IsNullOrEmpty(localEnvironment) ?
Directory.GetCurrentDirectory() :
Path.Combine(Environment.GetEnvironmentVariable("HOME"), "site", "wwwroot");
var testConfig = new ConfigurationBuilder()
.SetBasePath(workingDirectory)
.AddJsonFile("jsonconfgi1.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
_configuration = testConfig.Build();

}
}
}
```

* This overrides the host.json file configuration settings. The test for this is if a setting defined in host.json but not the added json file is set back to its default value.

**Diagnostic Level**
Warning

**How to Fix Violations**
Use IFunctionConfigurationBuilder to add additional configuration, as described here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources

```csharp
using System.IO;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]

namespace MyNamespace
{
public class Startup : FunctionsStartup
{
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
FunctionsHostBuilderContext context = builder.GetContext();

builder.ConfigurationBuilder
.AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), optional: true, reloadOnChange: false)
.AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"), optional: true, reloadOnChange: false)
.AddEnvironmentVariables();
}
}
}
```

**Provide Code Fix**
Unsure if possible; probably not.

**Additional Documentation**
https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources

**When to Suppress Analyzer Rule**
This rule should not be suppressed.

Contributor guide

Open the contributing guide

Research direction

Start by tracing analyzer handling for FunctionsStartup.Configure() and ConfigurationBuilder().AddJsonFile(). Compare the documented ConfigureAppConfiguration(IFunctionsConfigurationBuilder) pattern, then verify the analyzer warns when host.json settings can be overridden and does not require a code fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.