Background: Asp.Net Core website using Dependency Injection to relay needed services to various parts of the website
I have a service that is being added to the IServiceCollection in the ConfigureServices method in my Startup class as a singleton in the following manner:
//Settings
services.AddSingleton<ISettingsService, SettingsService>();
services.AddSingleton<ISettingsIO, SettingsIO>();
services.AddSingleton<ISettings>(f =>
{
return f.GetService<ISettingsService>().GetSettings();
});
This works great and all the pages/controllers I need to have access to Example can do so without issue.
However, I now have the capability to change the data that is being pulled in the method GetSettings(). This means that I need to update the service that is added to the ServiceCollection.
How can I do this without changing the service from a singleton to transient?
Thanks for any help provided!
GetSettingsimplemented?ISettingis also as singleton sinceISettingsServiceis allready. Why not use it as transient or scoped and let theSettingsServicewho is singleton resolve it ?GetSettingsreturn a smart implementation ofISettings. Such implementation starts with an initial set of settings (the ones from the database) but that can then return a new set of settings. This implementation would provide means for you to set the new settings.