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

时间:2018-06-28 09:31:36

标签: c# .net entity-framework hangfire

我已经使用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;
        }
    }

我遇到上述错误,无法转换为空。我想通过发送任何方法作为参数来调用它。

1 个答案:

答案 0 :(得分:0)

我通过更改

之类的方法来做到这一点
 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;
        }
    }

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