0

I have a class library project which i am referring from my main console application (The class i am using is ProcessService). If the constructor would take only ILogger as an argument like this:

public ProcessService(ILogger factory)
    {

    }

And then i register it in ConfigureServices(IServiceCollection services) Like this:

services.AddSingleton<IProcessService, ProcessService>();

It works fine. The problem is that i need to pass other then ILogger another argument which i take from appsettings so the constructor of the class looks like this:

public ProcessService(int period,ILogger factory){}

And then im trying to register it like this:

services.AddSingleton<IProcessService>(new ProcessService(refreshInterval*1000,ILogger logger));

But i have to pass an initialized logger and its not correct. What is the way to achieve my requirement?

ATT
  • 921
  • 3
  • 13
  • 30
  • I believe you can achive what you want by using `services.AddSingleton(x => new ProcessService(refreshInterval*1000, x.GetRequiredService()));` Otherwise, please let me know and I will remove my vote to close as duplicate. – smoksnes Oct 03 '19 at 05:41
  • 1
    Yes it worked. Thank you. @smoksnes – ATT Oct 03 '19 at 05:48
  • please mark as duplicate if you believe that the solution given in the other answer effectively answered your question. – smoksnes Oct 03 '19 at 05:54

0 Answers0