Click or drag to resize
Job Class
Represents an action that can be marshalled to another process to be performed.
Inheritance Hierarchy
SystemObject
  Hangfire.CommonJob

Namespace: Hangfire.Common
Assembly: Hangfire.Core (in Hangfire.Core.dll) Version: 1.5.0.0
Syntax
public class Job

The Job type exposes the following members.

Constructors
  NameDescription
Public methodJob(MethodInfo)
Initializes a new instance of the Job class with the metadata of a method with no arguments.
Public methodJob(MethodInfo, Object)
Initializes a new instance of the Job class with the metadata of a method and the given list of arguments.
Public methodJob(Type, MethodInfo)
Initializes a new instance of the Job class with the type, metadata of a method with no arguments.
Public methodJob(Type, MethodInfo, Object)
Initializes a new instance of the Job class with the type, metadata of a method and the given list of arguments.
Public methodJob(Type, MethodInfo, String) Obsolete.
Initializes a new instance of the Job class
Top
Properties
  NameDescription
Public propertyArgs
Gets a read-only collection of arguments that Should be passed to a method invocation during the performance.
Public propertyMethod
Gets the metadata of a method that should be invoked during the performance.
Public propertyType
Gets the metadata of a type that contains a method that should be invoked during the performance.
Top
Methods
Remarks

The ability to serialize an action is the cornerstone of marshalling it outside of a current process boundaries. We are leaving behind all the tricky features, e.g. serializing lambdas with their closures or so, and considering a simple method call information as a such an action, and using reflection to perform it.

Reflection-based method invocation requires an instance of the MethodInfo class, the arguments and an instance of the type on which to invoke the method (unless it is static). Since the same MethodInfo instance can be shared across multiple types (especially when they are defined in interfaces), we also allow to specify a Type that contains the defined method explicitly for better flexibility.

Marshalling imposes restrictions on a method that should be performed:

  • Method should be public.
  • Method should not contain out and ref parameters.
  • Method should not contain open generic parameters.
Examples

The following example demonstrates the creation of a Job type instances using expression trees. This is the recommended way of creating jobs.

C#
public interface IMyInterface
{
    void Instance();
}

public class MyClass : IMyInterface
{
    public static void Static() { /* ... */ }
    public void Instance() { /* ... */ }
    public void WithArguments(string arg1, int arg2) { /* ... */ }
}

public void CreateJobs()
{
    var job1 = Job.FromExpression(() => MyClass.Static());

    var obj = new MyClass();
    var job2 = Job.FromExpression(() => obj.Instance());

    var job3 = Job.FromExpression<MyClass>(x => x.Instance());
    var job4 = Job.FromExpression<MyClass>(x => x.WithArguments("hello", 42));
    var job5 = Job.FromExpression<IMyInterface>(x => x.Instance());
}

The next example demonstrates unsupported methods. Any attempt to create a job based on these methods fails with NotSupportedException.

C#
// NotSupportedException – method is private
private void PrivateMethod() { }

// NotSupportedException – method contains parameter passed by reference.
public void MethodWithRefParameter(ref int arg) { }

// NotSupportedException – method contains output parameter.
public void MethodWithOutParameter(out int arg) { arg = 1; }

// NotSupportedException – method contains an open generic parameter.
public void MethodWithGenericParameter<T>(T arg) { }
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also