title: Azure function : Updating the config value throws exception (A configuration source is not registered. Please register one before setting a value.)
body: <p>I am trying to update the value of one of the config values in Configure(IWebJobsBuilder builder) method, it works fine when running locally but when deployed in azure function throws exception.</p>
  
  <p>Exception: </p>
  
  <pre><code>Message:  A configuration source is not registered. Please register one before setting a value.
  </code></pre>
  
  <p>stack trace : </p>
  
  <pre><code>System.InvalidOperationException:
     at Microsoft.Extensions.Configuration.ConfigurationRoot.set_Item (Microsoft.Extensions.Configuration, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at Microsoft.Extensions.Configuration.ChainedConfigurationProvider.Set (Microsoft.Extensions.Configuration, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at Microsoft.Extensions.Configuration.ConfigurationRoot.set_Item (Microsoft.Extensions.Configuration, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at Microsoft.Extensions.Configuration.ChainedConfigurationProvider.Set (Microsoft.Extensions.Configuration, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at Microsoft.Extensions.Configuration.ConfigurationRoot.set_Item (Microsoft.Extensions.Configuration, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at ConvAi.BfChannel.BotManagementService.WebJobsExtensionStartup.Configure (ConvAi.BfChannel.BotManagementService, Version=0.0.0.0, Culture=neutral, PublicKeyToken=nullConvAi.BfChannel.BotManagementService, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null: E:\Git\skyman - Copy\conversational-ai\src\ConvAi.BfChannel.BotManagementService\WebJobsExtensionStartup.csConvAi.BfChannel.BotManagementService, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null: 99)
  </code></pre>
  
  <p>I have logged  the config providers in app insights and I can see that there are 3 config providers registered.</p>
  
  <pre><code>1.  Microsoft.Extensions.Configuration.ChainedConfigurationProvider
  2.  Microsoft.Extensions.Configuration.AzureKeyVault.AzureKeyVaultConfigurationProvider
  3.  Microsoft.Extensions.Configuration.Memory.MemoryConfigurationProvider
  </code></pre>
  
  <p>Here is problematic code:</p>
  
  <pre><code>config["BotManagementServiceBusConnectionString"] =
                      config[serviceBusConnectionStringKey].Remove(
                          config[serviceBusConnectionStringKey].Length - ";EntityPath=Topic1".Length);
  </code></pre>
  
  <p>Here is the full code: </p>
  
  <pre><code>// --------------------------------------------------------------------------------------------------------------------
  // &lt;copyright file="WebJobsExtensionStartup.cs" company="Microsoft"&gt;
  // Copyright (c) Microsoft Corporation. All rights reserved.
  // &lt;/copyright&gt;
  //  --------------------------------------------------------------------------------------------------------------------
  
  using System;
  using System.Linq;
  using Intercom.Helpers;
  using Microsoft.ApplicationInsights.Extensibility;
  using Microsoft.Azure.KeyVault;
  using Microsoft.Azure.Services.AppAuthentication;
  using Microsoft.Azure.WebJobs;
  using Microsoft.Azure.WebJobs.Hosting;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.Configuration.AzureKeyVault;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Logging;
  
  [assembly: WebJobsStartup(typeof(ConvAi.BfChannel.BotManagementService.WebJobsExtensionStartup), "Web Jobs Extension")]
  namespace ConvAi.BfChannel.BotManagementService
  {
      /// &lt;summary&gt;
      /// WebJobsExtensionStartup
      /// &lt;/summary&gt;
      public class WebJobsExtensionStartup : IWebJobsStartup
      {
          /// &lt;summary&gt;
          /// AzureServiceTokenProvider which is used for requesting identity token.
          /// &lt;/summary&gt;
          public static AzureServiceTokenProvider AzureServiceTokenProvider { get; set; }
  
          /// &lt;summary&gt;
          /// Configure services.
          /// &lt;/summary&gt;
          /// &lt;param name="builder"&gt;WebJob Builder&lt;/param&gt;
          public void Configure(IWebJobsBuilder builder)
          {
              try
              {
                  bool isLocal = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"));
  
                  // Gets the default configuration
                  var serviceConfig = builder.Services.FirstOrDefault(s =&gt; s.ServiceType.Equals(typeof(IConfiguration)));
                  var rootConfig = (IConfiguration)serviceConfig.ImplementationInstance;
                  var keyVault = rootConfig["BFSpeechKeyVault"];
                  var serviceBusConnectionStringKey = rootConfig["BotManagementServiceBusConnectionStringKey"];
  
                  if (isLocal)
                  {
                      // Use developers's idenity.
                      AzureServiceTokenProvider = new AzureServiceTokenProvider();
                  }
                  else
                  {
                      // Use azure function's managed idenity.
                      var msiClientId = rootConfig["MSI_ClientId"];
                      AzureServiceTokenProvider = new AzureServiceTokenProvider(connectionString: $"RunAs=App;AppId={msiClientId}");
                  }
  
                  var keyVaultClient = new KeyVaultClient(
                      new KeyVaultClient.AuthenticationCallback(
                          AzureServiceTokenProvider.KeyVaultTokenCallback));
  
                  var config = new ConfigurationBuilder()
                      .AddConfiguration(rootConfig).AddAzureKeyVault(
                      keyVault,
                      keyVaultClient,
                      new DefaultKeyVaultSecretManager())
                      .AddInMemoryCollection()
                      .Build();
  
                  config["BotManagementServiceBusConnectionString"] =
                      config[serviceBusConnectionStringKey].Remove(
                          config[serviceBusConnectionStringKey].Length - ";EntityPath=Topic1".Length);
  
                  // Replace the existing config
                  builder.Services.AddSingleton&lt;IConfiguration&gt;(config);
              }
              catch (Exception ex)
              {
                  AppInsights.TrackException(
                      ex,
                      "Failed to start Bot management service",
                      "Sender".PairWith(this.GetType().FullName));
              }
          }
      }
  }
  </code></pre>
  
  <p>Can someone please help me in figuring out what is going on?</p>