title: How to modify values in the Options object (holds configuration settings) after it has been registered with dependency injection in ASP.NET Core?
body: <p>I have a method, one of many in different classes, example below, in a .NET class which uses a connection string, a property in an options object (Options Pattern), called <code>DBOptions</code>, which was injected into the class.</p>
  <p>DBOptions was registered in DI during app startup using <code>services.Configure&lt;DBOptions&gt;()</code>.</p>
  <p><code>DBOptions</code> during app startup is configurated to use a specific named connectionstring from <em>appsettings.json</em>.</p>
  <p>There's a need now to conditionally be able during run time to use another connection string. This is per web request by a user selection in the UI. I added a second named connection string in appsettings.json.</p>
  <p>Since currently during app startup DBoptions is set to use a specific named conenctionstring, how do I switch between the two connectionstrings during run time before DI injects DBOptions in a class? Or how do I update the DBOptions connectionstring before it gets injected into any class that uses it? AFAIK IOptionsSnapshot reloads the options when the source (appsettings.json file) gets modified which is not my case.</p>
  <p>App is not using EFCore.</p>
  <pre><code>public class Books
  {
      private readonly string connectionString;
      
      public Books(IOptions&lt;DBOptions&gt; options)
      {
          this.connectionString = options.Value.connectionString; //&lt;-- value needs to be set conditionally
                                                      //outside of any class that uses DBoptions
      }
      public void SomeMethod()
      {
          ....  //uses connectionString
      }
  }       
  </code></pre>