Object getting Null seems like deserialization issue in Hangfire

ぐ巨炮叔叔 提交于 2019-12-06 09:01:47

Hangfire does not attempt to store the state of the FileInFileOut object when the Enqueue method is called. It recreates the FileInFileOut object on the worker using a parameterless constructor (or one with parameters using a IOC activator) and then calls the method from your Enqueue expression. This results in the null reference (FileIn / FileOut not set).

You could modify your FileInFileOut method to pass the FileIn and FileOut objects to the Execute method (using a more functional programming approach). This may break other parts of your program so it's probably more appropraite to create a second wrapper class that instantiates the FileInFileOut object for you like:

public class FileInFileOutTasks
{
    public FileInFileOutTasks()
    {

    }
    public void RunExecute(FileIn fileIn, FileOut, fileout)
    {
        var scheduler = new FileInFileOut { FileIn = fileIn, FileOut = fileOut };
        scheduler.Execute();
    }
}

You would then call:

BackgroundJob.Enqueue<FileInFileOutTasks>(x => x.RunExecute(fileIn, fileOut));

and not even create the FileInFileOut object in your code if you didn't want to (just the FileIn / FileOut objects to be passed to the wrapper).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!