HangfireIO/Hangfire · error · ArgumentException

Argument count must be equal to method parameter count.

Error message

Argument count must be equal to method parameter count.

What it means

ArgumentException thrown by Job.Validate when method.GetParameters().Length != argumentCount — the number of arguments supplied to the Job constructor does not match the method's parameter count. Hangfire serializes arguments positionally and re-invokes by reflection, so a mismatch would cause a TargetParameterCountException at perform time; the validation catches it early at definition time. The parameter name reported is the argument array's name (e.g., 'args').

Source

Thrown at src/Hangfire.Core/Common/Job.cs:507

            if (!method.DeclaringType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
            {
                throw new ArgumentException(
                    $"The type `{method.DeclaringType}` must be derived from the `{type}` type.",
                    typeParameterName);
            }

            if (method.ReturnType == typeof(void) &&
                AsyncStateMachineAttributeCache.GetOrAdd(method, static m => m.GetCustomAttribute<AsyncStateMachineAttribute>()) != null)
            {
                throw new NotSupportedException("Async void methods are not supported. Use async Task instead.");
            }

            var parameters = method.GetParameters();

            if (parameters.Length != argumentCount)
            {
                throw new ArgumentException(
                    "Argument count must be equal to method parameter count.",
                    argumentParameterName);
            }

            foreach (var parameter in parameters)
            {
                // There is no guarantee that specified method will be invoked
                // in the same process. Therefore, output parameters and parameters
                // passed by reference are not supported.

                if (parameter.IsOut)
                {
                    throw new NotSupportedException(
                        "Output parameters are not supported: there is no guarantee that specified method will be invoked inside the same process.");
                }

                if (parameter.ParameterType.IsByRef)
                {

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Align the args array length with method.GetParameters().Length.
  2. Prefer Job.FromExpression which extracts arguments from the expression tree automatically, avoiding manual count errors.
  3. When changing a job method signature, expire old jobs or keep an overload to avoid deserialization mismatches.

Example fix

// before — count mismatch
var method = typeof(Worker).GetMethod("Run"); // void Run(int a, int b)
var job = new Job(typeof(Worker), method, new object[] { 1 });

// after — correct count
var job = new Job(typeof(Worker), method, new object[] { 1, 2 });
// or let FromExpression handle it
var job = Job.FromExpression<Worker>(x => x.Run(1, 2));
Defensive patterns

Strategy: validation

Validate before calling

if (method.GetParameters().Length != args.Length)
    throw new ArgumentException($"Argument count ({args.Length}) does not match method parameter count ({method.GetParameters().Length}).");

Type guard

public static bool ArgumentCountMatches(MethodInfo method, IReadOnlyList<object> args)
    => method != null && args != null && method.GetParameters().Length == args.Count;

Try / catch

try { var job = new Job(type, method, args); }
catch (ArgumentException ex) when (ex.Message.Contains("Argument count")) { /* align args length with parameters */ }

Prevention

When it happens

Trigger: Constructing new Job(type, method, new object[] { 1, 2 }) for a method with one parameter; passing too few/many args via reflection; a deserialized job whose stored arg count drifts from the method after a code change.

Common situations: Refactoring a job method to add/remove a parameter without migrating serialized jobs; building jobs from MethodInfo with a manually-constructed args array that is out of sync; version skew between the enqueueing process and the performing process.

Related errors


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