abpframework/abp · error · ArgumentException

Job args types cannot contain null.

Error message

Job args types cannot contain null.

What it means

Thrown by AbpBackgroundJobWorkerOptions.GetDedicatedWorkerLockName via Check.NotNullOrEmpty plus an explicit null-element scan: the jobArgsTypes array must be non-empty AND contain no null elements, otherwise it throws ArgumentException. This guards the hashing step (which reads t.FullName) from a NullReferenceException.

Source

Thrown at framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobWorkerOptions.cs:199

    }

    public AbpBackgroundJobWorkerOptions AddDedicatedWorker<TArgs1, TArgs2>()
    {
        return AddDedicatedWorker(typeof(TArgs1), typeof(TArgs2));
    }

    public AbpBackgroundJobWorkerOptions AddDedicatedWorker<TArgs1, TArgs2, TArgs3>()
    {
        return AddDedicatedWorker(typeof(TArgs1), typeof(TArgs2), typeof(TArgs3));
    }

    protected virtual string GetDedicatedWorkerLockName(Type[] jobArgsTypes)
    {
        Check.NotNullOrEmpty(jobArgsTypes, nameof(jobArgsTypes));

        if (jobArgsTypes.Any(t => t == null))
        {
            throw new ArgumentException("Job args types cannot contain null.", nameof(jobArgsTypes));
        }

        // Hash the (stable, sorted) full type names so the derived lock name stays short and within the
        // length limits of distributed lock providers (e.g. SQL Server sp_getapplock is limited to 255 chars).
        var key = string.Join(",", jobArgsTypes.Select(t => t.FullName).Distinct().OrderBy(n => n, StringComparer.Ordinal));
        return "AbpBackgroundJobDedicatedWorker:" + key.ToMd5();
    }
}

View on GitHub (pinned to 7ed43b1931)

Solutions

  1. Filter nulls out of the Type[] before calling AddDedicatedWorker.
  2. Fix the reflection/generic-type code that produced the null Type.
  3. Validate the array with a debug assert or a guard clause at the call site.
  4. Use the strongly-typed generic overloads (AddDedicatedWorker<TArgs>) to avoid runtime nulls.

Example fix

// before
var types = new[] { typeof(EmailJobArgs), maybeNullType };
options.AddDedicatedWorker(types); // throws 'cannot contain null'
// after
var types = new[] { typeof(EmailJobArgs), maybeNullType }
    .Where(t => t != null).ToArray();
options.AddDedicatedWorker(types);
Defensive patterns

Strategy: type-guard

Validate before calling

var clean = jobArgsTypes.Where(t => t != null).ToArray();
if (clean.Length != jobArgsTypes.Length) { /* fix the producer of nulls */ }
options.AddDedicatedWorker(clean);

Type guard

static bool HasNoNulls(Type[] types) => types != null && types.Length > 0 && types.All(t => t is not null);

Try / catch

try { options.AddDedicatedWorker(types); }
catch (ArgumentException ex) when (ex.Message.Contains("cannot contain null"))
{ /* filter nulls / fix reflection that produced them */ }

Prevention

When it happens

Trigger: Calling AddDedicatedWorker with a Type[] that contains a null entry (e.g. typeof(SomeGeneric<>).MakeGenericType(...) returning null, or an array built with an unset slot).

Common situations: Building the Type[] dynamically where a reflection call yields null; generic type construction failure silently producing null; a default(Type) placeholder left in the array; passing new Type[] { null } by mistake.

Related errors


AI-assisted analysis of abpframework/abp@7ed43b1931 (2026-08-13). Data as JSON: /api/errors/7b3205e2db5abeac. Report an issue: GitHub.