question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using appsettings and not loosing host.json values

See original GitHub issue

Is your question related to a specific version? If so, please specify:

Latest host for V3 of functions

What language does your question apply to? (e.g. C#, JavaScript, Java, All)

C#

Question

I have implemented the changes as discussed in this issue #4464 from @martinfletcher but it doesn’t seem to work and my host.json is still being overwritten with my extension settings lost.

There seems to be no ImplementationInstance when reading the IConfiguration from the services.

public override void Configure(IFunctionsHostBuilder builder)
 {
            ConfigureServices(builder.Services);
}
public void ConfigureServices(IServiceCollection services)
{
    var providers = new List<IConfigurationProvider>();
    foreach (var descriptor in services.Where(descriptor => descriptor.ServiceType == typeof(IConfiguration)).ToList())

    {
        var existingConfiguration = descriptor.ImplementationInstance as IConfigurationRoot;
        if (existingConfiguration is null)
        {
            continue;
        }
        providers.AddRange(existingConfiguration.Providers);
        services.Remove(descriptor);
    }

    var executioncontextoptions = services.BuildServiceProvider()
        .GetService<IOptions<ExecutionContextOptions>>().Value;
    var currentDirectory = executioncontextoptions.AppDirectory;

    var config = new ConfigurationBuilder()
        .SetBasePath(currentDirectory)
        .AddJsonFile("appSettings.json", optional: false, reloadOnChange: true)
        .AddEnvironmentVariables();

    providers.AddRange(config.Build().Providers);

    services.AddSingleton<IConfiguration>(new ConfigurationRoot(providers));
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
tekkingcommented, Jan 27, 2020

For anyone else with this problem: We ran into a similar issue (overriding our host.json settings by injecting a custom IConfiguration) after moving to v3. We used to add the implementationinstance when running v2 functions, but it seems they moved away from that to an implementationfactory with v3 functions.

The easiest way we found to add to IConfiguration without overriding host.json was by requesting IConfiguration from a serviceProvider in startup as follows (example from our function startup):

            var serviceProvider = builder.Services.BuildServiceProvider();
            var existingConfig = serviceProvider.GetService<IConfiguration>();

            var rootPath = Directory.GetCurrentDirectory();
            var config = new ConfigurationBuilder()
                .AddConfiguration(existingConfig)
                .SetBasePath(rootPath)
                .AddJsonFile("local.settings.json", true)
                .AddAzureKeyVault($"https://{keyvaultName}.vault.azure.net/")
                .Build();

            builder.Services.Replace(new ServiceDescriptor(typeof(IConfiguration), config));
0reactions
brettsamcommented, Jan 28, 2020

There wasn’t a known issue here, but it looks like .NET changed how IConfiguration is registered in v3 (from instance to factory as @tekking pointed out):

https://github.com/dotnet/extensions/pull/1361/commits/a8a3cc16247471077566fb70e951ff444240e3ac#diff-9e9cba0278df6f1227eb36f7be70598b

This is a tricky way to handle configuration, and we’ve never liked it because it’s susceptible to changes outside of our control like this. If it works for you, then that’s great (which is why we haven’t blocked it), but we have a feature in PR now that will allow you to add configuration sources to the builder at the correct point in the host setup. I’m hoping to get it in within the next few weeks.

For reference:

Because there’s a workaround and it’s outside of our control, I’m going to close this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Host.json not applying to Azure Application Settings ...
For some reason, the settings under "Values" are not applying in the Azure portal. When I view the function settings none of these...
Read more >
Why isn't my ASP.NET Core environment-specific ...
I was recently standing up a new ASP.NET Core application running in Docker, and I was seeing some very strange behaviour.
Read more >
host.json reference for Azure Functions 2.x and later
For a reference of host.json in Functions 1.x, see host.json reference for Azure Functions ... Don't reduce value while you're debugging.
Read more >
Learn how YOU can manage your app configuration in ...
Host configuration is the first thing to be read followed by JSON data found in appsettings.json and an environment specific version.
Read more >
App settings reference for Azure Functions
When Functions runs locally, app settings are specified in the Values collection in the local.settings.json. There are other function app ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found