AppSettings setup issue

1 month ago
Hi all,

I want to add new variables to my appsettings.json file and refer them in my project.
I don`t want to add them or see them in the Admin section, only to successfully refer them in my project. The end goal is to make them dynamic, so they can be replaced as part of the CI/CD process.

Unfortunately, I have tried many things and nothing works, can you please help me

Thanks all!
1 month ago
What nopCommerce version?
Are you customizing the solution or trying to do it from a plugin?
1 month ago
With Nop4.6 you can implement it like this

1. Add your settings in appSettings. For example,  you have 2 settings, Setting1 and Setting2
"CustomConfig": {
  "Setting1": "String value",
  "Setting2": true
}

2. Create a new CustomConfig class
public class CustomConfig : IConfig
{
     /// <summary>
/// Gets or sets a value for custom setting1
/// </summary>
public string Setting1 { get; private set; }

/// <summary>
/// Gets or sets a value for custom setting2
/// </summary>
public bool Setting2 { get; private set; }
}


3. Inject the AppSettings at your constructor and access the setting

public partial class MyController : BasePublicController
{
     private readonly AppSettings _appSettings;

     public MyController(AppSettings appSettings)
     {
         _appSettings = appSettings;
     }
     public virtual IActionResult Index()
     {
         string setting1 = _appSettings.Get<CustomConfig>().Setting1;
         bool setting2 = _appSettings.Get<CustomConfig>().Setting2;

         return View();
     }
}

1 month ago
Thanks all for trying to help!
I am using NopCommerce 4.6.
Actually what Sanju Dahal shared is what I am trying now, but the issue I am getting is when I try to access those variables like this:
         bool setting2 = _appSettings.Get<CustomConfig>().Setting2;

When I try to see all the variables in the appsettings list, I actually see the ones I want to use, but they are not replaced as I expect them to be replaced and thats my main issue, it says that the variable was not in the list, when in fact it was. So I am wondering if Im doing something wrong
1 month ago
Are you changing the appSetting's value in the runtime?
The app setting value is parsed during the application load and the changes will only be reflected once the application is restarted.  
If you are changing the appSettings.json in runtime, you must read the appSettings.json using File.ReadAllText and parse the value with Newtonsoft.Json
1 month ago
Hi, thanks for your reply, I am not changing items in runtime, but what I figured out is that if I create the files in Nop.Core project their values will be able to be fetched properly.

The next thing I plan to investigate is how can I fetch those without adding customizations to that project. Do you know how that portion works?
1 month ago
One consideration is, where are you using these "variables"?  Are they needed as part of the Startup process?  If not, consider using Settings (in the DB).
1 week ago
yoanna.kostova wrote:
Thanks all for trying to help!
I am using NopCommerce 4.6.
Actually what Sanju Dahal shared is what I am trying now, but the issue I am getting is when I try to access those variables like this:
         bool setting2 = _appSettings.Get<CustomConfig>().Setting2;

When I try to see all the variables in the appsettings list, I actually see the ones I want to use, but they are not replaced as I expect them to be replaced and thats my main issue, it says that the variable was not in the list, when in fact it was. So I am wondering if Im doing something wrong


We have the same issue. We dont want any secrets or connectionstrings in appsettings so we need a IConfig which can be registered on startup. The reason for this is so that user-secrets can be inserted automatically.

I ended up having my config class i nop.core. Then everything works fine using user-secrets and appsettings in my web app in azure. This is not optimal, but it works for now.

If i register the class in my plugin i get the same issue as someone else that the key does not exist in the dictionary.
1 week ago
sanju.dahal741 wrote:
With Nop4.6 you can implement it like this

1. Add your settings in appSettings. For example,  you have 2 settings, Setting1 and Setting2
"CustomConfig": {
  "Setting1": "String value",
  "Setting2": true
}

2. Create a new CustomConfig class
public class CustomConfig : IConfig
{
     /// <summary>
/// Gets or sets a value for custom setting1
/// </summary>
public string Setting1 { get; private set; }

/// <summary>
/// Gets or sets a value for custom setting2
/// </summary>
public bool Setting2 { get; private set; }
}


3. Inject the AppSettings at your constructor and access the setting

public partial class MyController : BasePublicController
{
     private readonly AppSettings _appSettings;

     public MyController(AppSettings appSettings)
     {
         _appSettings = appSettings;
     }
     public virtual IActionResult Index()
     {
         string setting1 = _appSettings.Get<CustomConfig>().Setting1;
         bool setting2 = _appSettings.Get<CustomConfig>().Setting2;

         return View();
     }
}



Right. This is how IConfig settings are supposed to work. But at the moment this approach only works for IConfig in the core, not in plugins. We are looking for a solution, details here.

For now, you can register such settings directly in the plugin, so place this code in plugin INopStartup.ConfigureServices():

var config = new CustomConfig();
configuration.GetSection(config.Name).Bind(config, options => options.BindNonPublicProperties = true);
AppSettingsHelper.SaveAppSettings(new List<IConfig> { config }, CommonHelper.DefaultFileProvider, false);
1 week ago
RomanovM wrote:
With Nop4.6 you can implement it like this

1. Add your settings in appSettings. For example,  you have 2 settings, Setting1 and Setting2
"CustomConfig": {
  "Setting1": "String value",
  "Setting2": true
}

2. Create a new CustomConfig class
public class CustomConfig : IConfig
{
     /// <summary>
/// Gets or sets a value for custom setting1
/// </summary>
public string Setting1 { get; private set; }

/// <summary>
/// Gets or sets a value for custom setting2
/// </summary>
public bool Setting2 { get; private set; }
}


3. Inject the AppSettings at your constructor and access the setting

public partial class MyController : BasePublicController
{
     private readonly AppSettings _appSettings;

     public MyController(AppSettings appSettings)
     {
         _appSettings = appSettings;
     }
     public virtual IActionResult Index()
     {
         string setting1 = _appSettings.Get<CustomConfig>().Setting1;
         bool setting2 = _appSettings.Get<CustomConfig>().Setting2;

         return View();
     }
}



Right. This is how IConfig settings are supposed to work. But at the moment this approach only works for IConfig in the core, not in plugins. We are looking for a solution, details here.

For now, you can register such settings directly in the plugin, so place this code in plugin INopStartup.ConfigureServices():

var config = new CustomConfig();
configuration.GetSection(config.Name).Bind(config, options => options.BindNonPublicProperties = true);
AppSettingsHelper.SaveAppSettings(new List<IConfig> { config }, CommonHelper.DefaultFileProvider, false);


Thank you for that!