HangfireIO/Hangfire · error · ArgumentNullException

context

Error message

context

What it means

Thrown by JobParameterInjectionFilter.OnPerforming when the PerformingContext parameter is null. This IServer filter injects job parameters (marked with [FromParameter]) into method arguments before execution; it requires a valid context to access BackgroundJob and its arguments.

Source

Thrown at src/Hangfire.Core/JobParameterInjectionFilter.cs:30

// 
// You should have received a copy of the GNU Lesser General Public 
// License along with Hangfire. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Reflection;
using Hangfire.Common;
using Hangfire.Server;
using Hangfire.States;

namespace Hangfire
{
    public class JobParameterInjectionFilter : IServerFilter
    {
        internal static readonly string DefaultException = "OCE";

        public void OnPerforming(PerformingContext context)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));

            var argumentsArray = context.BackgroundJob.Job?.Args as object[];
            if (argumentsArray == null) return;

            var parameters = context.BackgroundJob.Job.Method.GetParameters();

            for (var index = 0; index < parameters.Length; index++)
            {
                var attribute = parameters[index].GetCustomAttribute<FromParameterAttribute>();
                if (attribute == null) continue;

                var parameterType = parameters[index].ParameterType;
                var parameterName = attribute.ParameterName;

                if (String.IsNullOrEmpty(parameterName)) continue;

                var serialized = context.Connection.GetJobParameter(context.BackgroundJob.Id, parameterName);
                if (serialized != null)

View on GitHub (pinned to c236dd0f93)

Solutions

  1. In tests, construct a valid PerformingContext before calling OnPerforming
  2. Ensure the filter is registered through the standard Hangfire pipeline (not invoked manually with null)
  3. If calling the filter programmatically, guard against null context before invocation

Example fix

// before (in a test)
filter.OnPerforming(null);

// after
var context = new PerformingContext(storage, connection, job, ...);
filter.OnPerforming(context);
Defensive patterns

Strategy: validation

Validate before calling

if (context == null)
    throw new InvalidOperationException("PerformingContext is required for OnPerforming.");
filter.OnPerforming(context);

Type guard

context != null && context is PerformingContext

Prevention

When it happens

Trigger: Calling OnPerforming(null) directly, or a custom server filter pipeline that passes null to this filter. In normal Hangfire operation, the framework always provides a non-null PerformingContext.

Common situations: Unit testing the filter without constructing a real PerformingContext, or a misconfigured server filter pipeline that drops the context. Extremely unlikely in production.

Related errors


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