I have created a worker service to schedule a task using Coravel is a .NET Standard library and it is working as expected if I don't use the extra parameter with the string. I want to host the same as a windows service.
Program.cs
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
BuildConfig(builder);
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Build())
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddScheduler();
services.AddTransient<CoravelService>();
})
.UseSerilog()
.Build();
host.Services.UseScheduler(scheduler =>
{
var jobSchedule = scheduler.Schedule<CoravelService>();
jobSchedule.EverySecond();
});
host.Run();
}
CoravelService.cs
public CoravelService(ILogger<CoravelService> log, IConfiguration configuration)
{
_log = log ??
throw new ArgumentNullException(nameof(log));
_configuration = configuration ??
throw new ArgumentNullException(nameof(configuration));
}
public Task Invoke()
{
var testGuid = "test";
_log.LogError(testGuid.ToString());
return Task.FromResult(true);
}
And its works perfectly, but problem begins when i want to have one more extra parameter in Coravel Service.
CoravelService.cs
public CoravelService(ILogger<CoravelService> log, IConfiguration configuration, string someArgument)
{
_log = log ??
throw new ArgumentNullException(nameof(log));
_configuration = configuration ??
throw new ArgumentNullException(nameof(configuration));
_someArgument = someArgument;
}
public Task Invoke()
{
_log.LogError(_someArgument);
return Task.FromResult(true);
}
In this situantion constructor is not initializing, i think the reason of this situation is that i should somewhere pass this string, but i dont know where and how.
I got this working by implementing
IInvocableWithPayload<T>on my invocable class and usingscheduler.ScheduleWithParams()to add jobs to the schedule.Using the example from the question it would look like this:
Program.cs relevant part
CoravelService.cs