Base solution for your next web application
Open Closed

SettingManager is null in derived PeriodicBackgroundWorker #1742


User avatar
0
paul lee created

Hi All,

I am trying to add a Background Worker that will be executed according to a schedule, I am using HangFire integration.

Inside this BackgroundWorker I need to get some settings from DB, I am using the Setting infrastructure build into Abp. However, I couldn't seems to get the SettingManager populated and it is always null.

I have followed the Documentation:

  1. I have defined a new namespace: MyApp.Configuration in Core module. Inside this namespace there are 2 new classes, MyAppSettingsProvider and MyAppSettingNames.

  2. Since the Backgound Worker will be used in the Web module I have added the following to WebModule class in the PreInitialize() method:

public override void PreInitialize()
    {
      //Enable database based localization
      // Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
      Configuration.Localization.Languages.Add(new LanguageInfo("en", "English", "famfamfam-flag-england", true));

      .....

      Configuration.Settings.Providers.Add<MyAppSettingsProvider>();

      Configuration.BackgroundJobs.UseHangfire(configuration =>
      {
        configuration.GlobalConfiguration.UseSqlServerStorage("Default");
      });
    }
  1. I am reading the setting during the constructor of the background worker, but I am getting Null Reference Exception:
public class MyPassiveWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
  {
    private readonly string _workingPath;

    public MyPassiveWorker(AbpTimer timer) : base(timer)
    {
      _workingPath = SettingManager.GetSettingValue(MyAppSettingNames.FuntionName_WorkingDirectory);
      Timer.Period = 60000;
    }

I tried to debug into the SettingManager.GetSettingValue.... line and SettingManager is always null. So what am I missing?

Thanks you very much.

Paul


2 Answer(s)
  • User Avatar
    0
    paul lee created

    Hi All,

    Ok, I have re-read the documentation page and kinda missed the injecting the ISettingManager to my BackgroundWorker and from the constructor I can then assigned it to the property exposed by BackgroundWorkerBase base class.

    However, I read on top of the documentation page that I need to implement ISettingStore before the settings will be read from database. It also talked about Module Zero already implemented this.

    So, in my project I am also using Module Zero, then do I need to implement ISettingStore or I can just used the implementation from Module Zero?

    Would be great if someone can point me to the right direction.

    Thank you very much.

    Paul

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    ISettingStore interface is fully implemented in module zero <a class="postlink" href="https://github.com/aspnetboilerplate/module-zero/blob/1c77cfe842f81170f4ce7e10036c1e8784331dc6/src/Abp.Zero/Configuration/SettingStore.cs">https://github.com/aspnetboilerplate/mo ... ngStore.cs</a>.

    You dont have to implement in unless you have a special requirement like storing/reading settings fron another source than a database.