Job Class |
Namespace: Hangfire.Common
The Job type exposes the following members.
Name | Description | |
---|---|---|
Job(MethodInfo) |
Initializes a new instance of the Job class with the
metadata of a method with no arguments.
| |
Job(MethodInfo, Object) |
Initializes a new instance of the Job class with the
metadata of a method and the given list of arguments.
| |
Job(Type, MethodInfo) |
Initializes a new instance of the Job class with the
type, metadata of a method with no arguments.
| |
Job(Type, MethodInfo, Object) |
Initializes a new instance of the Job class with the
type, metadata of a method and the given list of arguments.
| |
Job(Type, MethodInfo, String) | Obsolete. Initializes a new instance of the Job class |
Name | Description | |
---|---|---|
Args |
Gets a read-only collection of arguments that Should be passed to a
method invocation during the performance.
| |
Method |
Gets the metadata of a method that should be invoked during the
performance.
| |
Type |
Gets the metadata of a type that contains a method that should be
invoked during the performance.
|
Name | Description | |
---|---|---|
FromExpression(ExpressionAction) |
Gets a new instance of the Job class based on the
given expression tree of a method call.
| |
FromExpressionTType(ExpressionActionTType) |
Gets a new instance of the Job class based on the
given expression tree of an instance method call with explicit
type specification.
| |
ToString | (Overrides ObjectToString.) |
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:
The following example demonstrates the creation of a Job type instances using expression trees. This is the recommended way of creating jobs.
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.
// 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) { }