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 DBOptions, which was injected into the class.
DBOptions was registered in DI during app startup using services.Configure<DBOptions>().
DBOptions during app startup is configurated to use a specific named connectionstring from appsettings.json.
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.
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.
App is not using EFCore.
public class Books
{
private readonly string connectionString;
public Books(IOptions<DBOptions> options)
{
this.connectionString = options.Value.connectionString; //<-- value needs to be set conditionally
//outside of any class that uses DBoptions
}
public void SomeMethod()
{
.... //uses connectionString
}
}