Last post Jun 28, 2019 09:37 AM by Sherry Chen
Jun 27, 2019 10:45 AM|ajithegde007|LINK
I created an REST api application which has many settings and stored in database. These settings are used during filtering and inserting data to the table.
Because I need to access settings every time I need to insert data. Instead of accessing settings from database, I created a global settings class and I put every settings in that class.
public static class GlobalSettings
public static string Setting_1;
public static string Setting_2;
public static string Setting_3;
public static string Setting_4;
public static void Initialize(ISettingsRepo repo)
var settings = new GSettings(repo);
Setting_1 = settings.SetSetting_1();
Setting_2 = settings.SetSetting_2();
Setting_3 = settings.SetSetting_3();
Setting_4 = settings.SetSetting_4();
catch (Exception ex)
throw new Exception("Error when loading settings.\r\n" + ex.Message);
Here ISettingsRepo is scoped service that will load the settings from database. The functions will initialize the settings to the properties. The functions will initialize the settings to the properties.
Now to initialize GlobalSettings I used configure method in startup class like this.
using (var scope = app.ApplicationServices.CreateScope())
Now I can use this in controller or anywhere in my api and get settings without accessing database.
Also I tried to use CustomConfigurationProvider. While it is good, I am unable to update the settings without restarting the server. So I didn't use CustomConfigurationProvider.
With the above method I can reload the settings from database any time I want from admin controller. But does this method correct way or has memory leak problems?
Is there any better method to do this.?
Jun 28, 2019 09:37 AM|Sherry Chen|LINK
Hi ajithegde007 ,
How will you use GlobalSettings while inserting data? How did you reload GlobalSettings if there is setting updated? You should avoid using static object. The custom
configuration provider example shows how to create a basic configuration provider that reads configuration key-value pairs from a database using Entity Framework (EF).
Best Regards ,