对象获取Null似乎是Hangfire中的反序列化问题 [英] Object getting Null seems like deserialization issue in Hangfire

查看:118
本文介绍了对象获取Null似乎是Hangfire中的反序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

It seems like Hangfire is unable to deserialize my original Scheduler object with all its state, whose Execute method I am calling in BackgroundJob.Enqueue() as shown below:

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

调度程序的类型为IScheduler,如下所示:

The Scheduler is of type IScheduler as shown below:

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

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

Is it some kind of deserialization issue? How to identify and correct this issue?

推荐答案

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

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).

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

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

然后您将致电:

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

甚至不愿意在代码中创建FileInFileOut对象(只是将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).

这篇关于对象获取Null似乎是Hangfire中的反序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆