MassTransit/MassTransit · error · ArgumentNullException

values

Error message

values

What it means

PublishInitializer requires a non-null initializer object (the anonymous object/dictionary whose properties initialize message T); when values is null it throws ArgumentNullException("values"). A null initializer cannot produce a message instance.

Solutions

  1. Ensure a concrete initializer is passed, e.g. publishEndpoint.Publish<OrderSubmitted>(new { OrderId = ... }).
  2. Null-check values before calling and handle the empty case.
  3. Fix upstream mapping so the initializer is always constructed.
  4. If no data is needed, publish a fully constructed message type instead of a null initializer.

Example fix

// before
object values = BuildInitializer(); // returns null
await publishEndpoint.PublishInitializer(publishEndpoint, values);
// after
var values = new { OrderId = id };
await publishEndpoint.Publish<OrderSubmitted>(values);
Defensive patterns

Strategy: validation

Validate before calling

if (values is null)
    throw new ArgumentNullException(nameof(values), "Initializer object must not be null.");

Type guard

bool HasInitializer(object? v) => v is not null;

Try / catch

try
{
    await publishEndpoint.PublishInitializer(publishEndpoint, values, cancellationToken);
}
catch (ArgumentNullException ex) when (ex.ParamName == "values")
{
    logger.LogError(ex, "Null initializer passed to PublishInitializer");
}

Prevention

When it happens

Trigger: Calling the initializer-based publish with values == null, e.g. an anonymous object expression that evaluated to null or a dictionary that was never built.

Common situations: Passing the result of a failed mapping; a variable typed object holding null from deserialization; refactor that removed the anonymous-object construction.

Related errors


AI-assisted analysis of MassTransit/MassTransit@62ab339afa (2026-09-13). Data as JSON: /api/errors/f7b8585ea5585e50. Report an issue: GitHub.

Appendix: source

Thrown at src/MassTransit.Abstractions/Contexts/Context/PublishEndpointConverterCache.cs:109

                if (endpoint == null)
                    throw new ArgumentNullException(nameof(endpoint));
                if (message == null)
                    throw new ArgumentNullException(nameof(message));
                if (pipe == null)
                    throw new ArgumentNullException(nameof(pipe));

                if (message is T msg)
                    return endpoint.Publish(msg, pipe, cancellationToken);

                throw new ArgumentException("Unexpected message type: " + TypeCache.GetShortName(message.GetType()));
            }

            public Task PublishInitializer(IPublishEndpoint endpoint, object values, CancellationToken cancellationToken)
            {
                if (endpoint == null)
                    throw new ArgumentNullException(nameof(endpoint));
                if (values == null)
                    throw new ArgumentNullException(nameof(values));

                return endpoint.Publish<T>(values, cancellationToken);
            }

            public Task PublishInitializer(IPublishEndpoint endpoint, object values, IPipe<PublishContext> pipe, CancellationToken cancellationToken)
            {
                if (endpoint == null)
                    throw new ArgumentNullException(nameof(endpoint));
                if (values == null)
                    throw new ArgumentNullException(nameof(values));
                if (pipe == null)
                    throw new ArgumentNullException(nameof(pipe));

                return endpoint.Publish<T>(values, pipe, cancellationToken);
            }
        }

View on GitHub (pinned to 62ab339afa)