In a .NET Core console app, I want to do this in Program.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices(serviceCollection =>
{
serviceCollection.AddSingleton(new MyService()); // error on this line
});
webBuilder.UseStartup<Startup>();
});
The error I am getting is:
There is no argument given that corresponds to the required formal parameter 'logger' of 'MyService(ILogger, IMediator)'
And I am not sure how I am supposed to add the 2 required params (the serilog Logger and the mediat-r instances) when I call the constructor of MyService
I need this because the service must be called once when I open / run the console...
You can use this code:
But it's not a good idea to inject instance of
IMediator
which have less lifetime period (I guess, because usually it's scoped) than yourMyService
.Some info: