0

My appsettings.json looks something like following: { "Server": "ABC", "DefaultDatabase": "XYZ", "delayTimer" : 10,}

I have another class scheduler.cs(Data layer) in which I am reading "delayTimer" from Appsettings in the constructor as follows:

    public class Scheduler
    {    
       public Scheduler(IConfiguration configSrc)
       {           
          this.delayTime = configSrc.GetValue<int>("delayTimer");
          CallScheduler();
       }
    }

Now, I need to call this class from "Startup.cs" when the application is loading, but how do I create an instance of it? I am aware that as the Scheduler class is having constructor dependency then I may need to pass object of a class which implements "IConfiguration" interface. In Startup.cs, I am trying following:

    public void ConfigureServices(IServiceCollection services)
    {            
        services.AddControllers();

        services.AddSingleton(typeof(Config));
        services.AddSingleton(typeof(TokenHelper));
               
        // The next line gives an error: I need to pass a class object
        // implementing IConfiguration interface but when I try to
        // create a new class which implements IConfiguration it result
        // in errors.
        Scheduler obj = new Scheduler();
        services.AddSingleton<Scheduler>(obj);

        Thread t1 = new Thread(new ThreadStart(obj.CallScheduler));
        t1.Start();

My question: How do I read the appsettings.json value of "delayTimer" in data layer class??. I read lot of posts which speaks about reading the content from asppsetting to controller class. Example: Read asppsetting.json to controller but I am not looking for this requirement.

Steven
  • 166,672
  • 24
  • 332
  • 435
abhishek
  • 29
  • 4
  • 1
    I would suggest you look at the Options pattern. This will allow you to configure a class to represent the settings which can be injected as IOptions where you need it. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0 – Ryan Thomas Oct 13 '21 at 08:52
  • 1
    Instead of letting `Scheduler` depend on the very broad `IConfiguration` interface, it's much better to make it's constructor as specific as possible. For instance by injecting the `delayTimer` `int` directly into the constructor, or by injecting a `SchedulerSettings` class that wraps this `DelayTimer`. The `delayTimer` can, in that case, be easily read from the configuration file from inside the `Startup` class of the startup project. – Steven Oct 13 '21 at 09:11
  • @RyanThomas , Even if I implement Options pattern and inject settings through constrcutor as : `public Scheduler(IOptions)` , How will I be able to initiate Scheduler Object so that i can "CallScheduler" method using this object?. – abhishek Oct 13 '21 at 12:01

2 Answers2

1

You can read config in configureService and can provide config as below

.ConfigureServices((hostContext, services) =>
                        {
                            var config = hostContext.Configuration;
                            Scheduler obj = new Scheduler(config);
                        }
decoder
  • 886
  • 22
  • 46
1

I could fetch the value directly to my data layer class using this: new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("MyCustom")["delayTimer"]

Where MyCustom is the section of JSON file and delayTimer is the key I want to read.

abhishek
  • 29
  • 4