无法从 'void' 转换为 System.Threading.Tasks.Task<System.Action>

Cannot convert from 'void' to System.Threading.Tasks.Task<System.Action>

我已经使用 hangfire .Net 编写了通用方法。基本上我想实现的是,我已经生成了一个方法,并希望在需要时多次调用它。我的辅助方法如下:

 public static void ScheduleBackGroundJob(Task<Action> _refmethod, DateTime _dateTime)
    {
        try
        {
            var _currentDate = DateTime.Now;
            TimeSpan _timeSpan = _dateTime - _currentDate;
            BackgroundJob.Schedule(() => _refmethod, _timeSpan);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我想像这样调用这个方法

HangfireHelper.ScheduleBackGroundJob(_service.UpdateAuctionStatus(result), result.PlannedCloseDate);

我的 updateAuctionStatus 方法如下。

public void UpdateAuctionStatus(AuctionReturnModel model)
    {
        try
        {
            var _auction = (from d in _db.Auction_Detail where d.Id == model.AuctionDetailId select d).FirstOrDefault();
            _auction.isEnded = true;
            _db.Auction_Detail.Add(_auction);
            _db.SaveChanges();

            var _auctionHistory = new AuctionHistory { AuctionDetail_Id = model.AuctionDetailId, EndedDate = DateTime.Now, EndedMethod = "Automatic" };
            _db.AuctionHistory.Add(_auctionHistory);
            _db.SaveChanges();

            Task.WaitAll();

        }
        catch (Exception ex)
        {
           throw ex;
        }
    }

我得到的上述错误无法转换为无效。我想通过将任何方法作为参数发送来调用它。

我已经通过改变我的方法做到了

 public static void ScheduleBackGroundJob(Expression<Action> _refmethod, DateTime _dateTime)
    {
        try
        {
            var _currentDate = DateTime.Now;
            TimeSpan _timeSpan = _dateTime - _currentDate;
            BackgroundJob.Schedule(_refmethod, _timeSpan);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

正在调用 Hangfire 助手代码

 HangfireHelper.ScheduleBackGroundJob(() => _service.UpdateAuctionStatus(result), result.PlannedCloseDate);

我的服务方式我改了

  public async Task UpdateAuctionStatus(AuctionReturnModel model)
    {
        try
        {
            var _auction = await (from d in _db.Auction_Detail where d.Id == model.AuctionDetailId select d).FirstOrDefaultAsync();
            _auction.isEnded = true;
            _db.Auction_Detail.Add(_auction);
            _db.SaveChanges();

            var _auctionHistory = new AuctionHistory { AuctionDetail_Id = model.AuctionDetailId, EndedDate = DateTime.Now, EndedMethod = "Automatic" };
            _db.AuctionHistory.Add(_auctionHistory);
            _db.SaveChanges();

            Task.WaitAll();

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

希望这对你以后有所帮助。任何代码审查和最佳实践都将受到高度赞赏,因为我一直在寻找新的更好的方法来解决问题。