对象获取 Null 似乎是 Hangfire 中的反序列化问题

Object getting Null seems like deserialization issue in Hangfire

Hangfire 似乎无法反序列化我的原始 Scheduler 对象及其所有状态,我在 BackgroundJob.Enqueue() 中调用其 Execute 方法,如下所示:

Scheduler = new FileInFileOut
{
    FileIn = new FileInput()
    {
        FileName = "SampleInput.txt",
        DirectoryPath = @"C:\Users\LENOVO\Desktop\iRule",
        FileFormat = new System.Collections.Generic.Dictionary<string, string>
        {
            {"X", "ParseInt(input.Substring(0, 2))"},
            {"Y", "ParseInt(input.Substring(3, 2))"},
            {"Z", "ParseInt(input.Substring(6, 2))"},
        }
    },
    FileOut = new FileOutput()
    {
        FileName = "SampleOutput.txt",
        DirectoryPath = @"C:\Users\LENOVO\Desktop\iRule"
    },
    Trigger = new FireAndForgetTrigger()
};
BackgroundJob.Enqueue(() => Scheduler.Execute());

Scheduler 是 IScheduler 类型,如下所示:

public interface IScheduler
{
    IEntityBaseDef EntityItemBase { get; set; }
    ITrigger Trigger { get; set; }
    RuleApp RuleApp { get; set; }
    Type JobType { get; set; }
    void Execute();
}

[Serializable()]
[DataContract()]
public abstract class Scheduler : EntityModel, IScheduler
{
    public abstract void Execute();
}

[Serializable()]
[DataContract()]
public class FileInFileOut : Scheduler, IFileInFileOut
{
    private FileInput _fileIn;
    public FileInput FileIn
    {
        get { return _fileIn; }
        set { SetProperty(ref _fileIn, value); }
    }

    private FileOutput _fileOut;
    public FileOutput FileOut
    {
        get { return _fileOut; }
        set { SetProperty(ref _fileOut, value); }
    }

    public override void Execute()
    {
        // ERROR on below line, FileIn object is null 
        var directoryInfo = new DirectoryInfo(FileIn.DirectoryPath); 
        var files = directoryInfo.GetFiles();
        if (FileIn.SearchSubDirectories)
        {
            var directories = directoryInfo.GetDirectories().Flatten(x => x.GetDirectories());
        }
    }
}

我当前的调度程序发布逻辑:

public void Publish()
{
    if (Scheduler != null)
    {
        var trigger = Scheduler.Trigger;
        if (trigger is IFireAndForgetTrigger)
        {
            BackgroundJob.Enqueue(() => Scheduler.Execute());
        }
        else if (trigger is IDelayedTrigger)
        {
            var delayedTrigger = (IDelayedTrigger)trigger;
            BackgroundJob.Schedule(() => Scheduler.Execute(), delayedTrigger.Delay);
        }
    }
}

这是某种反序列化问题吗?如何识别和纠正此问题?

调用 Enqueue 方法时,Hangfire 不会尝试存储 FileInFileOut 对象的状态。它使用无参数构造函数(或使用 IOC 激活器的带有参数的构造函数)在 worker 上重新创建 FileInFileOut 对象,然后从您的 Enqueue 表达式调用该方法。这导致空引用(未设置 FileIn / FileOut)。

您可以修改 FileInFileOut 方法以将 FileIn 和 FileOut 对象传递给 Execute 方法(使用更函数式的编程方法)。这可能会破坏程序的其他部分,因此创建第二个包装器 class 为您实例化 FileInFileOut 对象可能更合适:

public class FileInFileOutTasks
{
    public FileInFileOutTasks()
    {

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

然后您将调用:

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

如果您不想,甚至不要在代码中创建 FileInFileOut 对象(只是要传递给包装器的 FileIn/FileOut 对象)。