相关疑难解决方法(0)

在ASP.NET Core中添加数据库驱动的调度程序的正确位置是什么?

我添加了一个TimerStartup类的的ASP.Net Core应用。它会在某个时间段触发,并执行诸如记录示例文本的操作。我需要它能够执行数据库驱动的操作,例如将记录添加到表中。因此,我尝试AppDbContext从DI 获取信息,但它始终为null。请查看代码:

    public class Scheduler
{
    static Timer _timer;
    static bool _isStarted;
    static ILogger<Scheduler> _logger;
    const int dueTimeMin = 1;
    const int periodMin = 1;

    public static void Start(IServiceProvider serviceProvider)
    {
        if (_isStarted)
            throw new Exception("Currently is started");

        _logger = (ILogger<Scheduler>)serviceProvider.GetService(typeof(ILogger<Scheduler>));

        var autoEvent = new AutoResetEvent(false);
        var operationClass = new OperationClass(serviceProvider);
        _timer = new Timer(operationClass.DoOperation, autoEvent, dueTimeMin * 60 * 1000, periodMin * 60 * 1000);
        _isStarted = true;
        _logger.LogInformation("Scheduler started"); …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection startup asp.net-core

3
推荐指数
1
解决办法
607
查看次数

标签 统计

asp.net-core ×1

c# ×1

dependency-injection ×1

startup ×1