简体   繁体   English

.NET Core Worker服务宿主和Coravel Scheduler

[英].NET Core Worker service host and Coravel Scheduler

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.我创建了一个工作人员服务来使用 Coravel 是一个 .NET 标准库来安排任务,如果我不使用带有字符串的额外参数,它会按预期工作。 I want to host the same as a windows service.我想托管与 windows 服务相同的服务。

Program.cs程序.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 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.它工作得很好,但是当我想在 Coravel 服务中有一个额外的参数时,问题就开始了。

CoravelService.cs 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 using scheduler.ScheduleWithParams() to add jobs to the schedule.我通过在我的可调用 class 上实施IInvocableWithPayload<T>并使用scheduler.ScheduleWithParams()将作业添加到计划中来完成这项工作。

Using the example from the question it would look like this:使用问题中的示例,它看起来像这样:

Program.cs relevant part Program.cs相关部分

host.Services.UseScheduler(scheduler =>
{
    var jobSchedule = scheduler.ScheduleWithParams<CoravelService>("test");
    jobSchedule.EverySecond();
});
host.Run();

CoravelService.cs CoravelService.cs

public class CoravelService : IInvocable, IInvocableWithPayload<string>
{
    public ILogger _log { get; set; }
    public IConfiguration _configuration { get; set; }
    public string Payload { get; set; }

    public CoravelService(ILogger<CoravelService> log, IConfiguration configuration, string payload)
    {
        _log = log ??
            throw new ArgumentNullException(nameof(log));
        _configuration = configuration ??
            throw new ArgumentNullException(nameof(configuration));
        Payload = payload;
    }

    public Task Invoke()
    {
        _log.LogError(Payload);

        return Task.FromResult(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM