HangfireIO/Hangfire · error · ArgumentNullException

method

Error message

method

What it means

Thrown as ArgumentNullException by the obsolete Job(Type, MethodInfo, string[]) constructor when the 'method' MethodInfo argument is null. The method is required both for Validate() (which checks accessibility, generic parameters, and signature) and for later invocation; a null method makes the job un-executable.

Source

Thrown at src/Hangfire.Core/Obsolete/Job.Obsolete.cs:32

// License along with Hangfire. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Reflection;
using Hangfire.Annotations;
using Hangfire.Server;
using Hangfire.Storage;

// ReSharper disable once CheckNamespace
namespace Hangfire.Common
{
    partial class Job
    {
        [Obsolete("Please use Job(Type, MethodInfo, object[]) ctor overload instead. Will be removed in 2.0.0.")]
        public Job([NotNull] Type type, [NotNull] MethodInfo method, [NotNull] string[] arguments)
        {
            if (type == null) throw new ArgumentNullException(nameof(type));
            if (method == null) throw new ArgumentNullException(nameof(method));
            if (arguments == null) throw new ArgumentNullException(nameof(arguments));

            Validate(type, nameof(type), method, nameof(method), arguments.Length, nameof(arguments));

            Type = type;
            Method = method;
            Args = InvocationData.DeserializeArguments(method, arguments);
        }

        /// <exclude />
        [NotNull]
        [Obsolete("Please use `Args` property instead to avoid unnecessary serializations/deserializations. Will be deleted in 2.0.0.")]
        public string[] Arguments => InvocationData.SerializeArguments(Method, Args);

        /// <exclude />
        [Obsolete("This method is deprecated. Please use `CoreBackgroundJobPerformer` or `BackgroundJobPerformer` classes instead. Will be removed in 2.0.0.")]
        public object Perform(JobActivator activator, IJobCancellationToken cancellationToken)
        {

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Use the non-obsolete Job(Type, MethodInfo, object[]) overload or, better, Job.FromExpression(...) to capture the method safely at compile time.
  2. Null-check the MethodInfo returned by GetMethod and handle the not-found case explicitly before constructing the Job.
  3. When using GetMethod on an overloaded method, use the overload that accepts parameter Type[] to disambiguate.

Example fix

// before
var method = type.GetMethod("Run"); // null if not found
var job = new Job(type, method, arguments);

// after
var job = Job.FromExpression<MyJob>(x => x.Run("arg"));
Defensive patterns

Strategy: validation

Validate before calling

if (method == null) throw new ArgumentNullException(nameof(method));
var job = new Job(type, method, arguments);

Type guard

static MethodInfo SafeGetMethod(Type t, string name, Type[] args) => t.GetMethod(name, args);

Prevention

When it happens

Trigger: Calling 'new Job(type, null, arguments)' — e.g. the MethodInfo was obtained via reflection (GetMethod) that returned null because the method name was misspelled, the binding flags were too restrictive, or the method is overloaded without disambiguation.

Common situations: Reflective job construction where type.GetMethod("DoWork") returns null (no such method, or ambiguous overload); obfuscation/proguard-style renaming that changes the method name at runtime; dynamic code that builds jobs from configuration and passes a null method on misconfiguration.

Related errors


AI-assisted analysis of HangfireIO/Hangfire@c236dd0f93 (2026-08-13). Data as JSON: /api/errors/47a46bc6433b3065. Report an issue: GitHub.