获取Null的对象似乎是Hangfire

时间:2017-06-16 13:11:14

标签: c# hangfire

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

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());
        }
    }
}

My Current Scheduler Publishing Logic:

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);
        }
    }
}

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

1 个答案:

答案 0 :(得分:4)

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

您可以修改FileInFileOut方法以将FileIn和FileOut对象传递给Execute方法(使用更多功能的编程方法)。这可能会破坏程序的其他部分,因此可能更适合创建第二个为您实例化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对象传递给包装器)。