reactiveui/refit · error · ArgumentNullException

The content serializer can't be null

Error message

The content serializer can't be null

What it means

The RefitSettings constructor throws ArgumentNullException when contentSerializer is null, because every request serialization path depends on an IHttpContentSerializer. All other constructor parameters are optional and default to built-in implementations, but the content serializer is mandatory.

Source

Thrown at src/Refit/RefitSettings.cs:75

        : this(contentSerializer, urlParameterFormatter, formUrlEncodedParameterFormatter, null)
    {
    }

    /// <summary>Initializes a new instance of the <see cref="RefitSettings"/> class.</summary>
    /// <param name="contentSerializer">The <see cref="IHttpContentSerializer"/> instance to use.</param>
    /// <param name="urlParameterFormatter">The <see cref="IUrlParameterFormatter"/> instance to use (defaults to <see cref="DefaultUrlParameterFormatter"/>).</param>
    /// <param name="formUrlEncodedParameterFormatter">The <see cref="IFormUrlEncodedParameterFormatter"/> instance to use (defaults to <see cref="DefaultFormUrlEncodedParameterFormatter"/>).</param>
    /// <param name="urlParameterKeyFormatter">The <see cref="IUrlParameterKeyFormatter"/> instance to use (defaults to <see cref="DefaultUrlParameterKeyFormatter"/>).</param>
    /// <exception cref="ArgumentNullException"><paramref name="contentSerializer"/> is <see langword="null"/>.</exception>
    public RefitSettings(
        IHttpContentSerializer contentSerializer,
        IUrlParameterFormatter? urlParameterFormatter,
        IFormUrlEncodedParameterFormatter? formUrlEncodedParameterFormatter,
        IUrlParameterKeyFormatter? urlParameterKeyFormatter)
    {
        ContentSerializer =
            contentSerializer
            ?? throw new ArgumentNullException(
                nameof(contentSerializer),
                "The content serializer can't be null");
        UrlParameterFormatter = urlParameterFormatter ?? new DefaultUrlParameterFormatter();
        FormUrlEncodedParameterFormatter =
            formUrlEncodedParameterFormatter ?? new DefaultFormUrlEncodedParameterFormatter();
        UrlParameterKeyFormatter =
            urlParameterKeyFormatter ?? new DefaultUrlParameterKeyFormatter();
        ExceptionFactory = new DefaultApiExceptionFactory(this).CreateAsync;
        TransportExceptionFactory = DefaultTransportExceptionFactory();
    }

    /// <summary>Gets or sets a function to provide the Authorization header. Does not work if you supply an HttpClient instance.</summary>
    public Func<
        HttpRequestMessage,
        CancellationToken,
        ValueTask<string>
    >? AuthorizationHeaderValueGetter
    { get; set; }

View on GitHub (pinned to b455f65ecc)

Solutions

  1. Pass a concrete IHttpContentSerializer, e.g. new SystemTextJsonContentSerializer()
  2. If loading it from config, provide a sensible default when the config value is absent
  3. Use a non-null DI registration for IHttpContentSerializer before resolving RefitSettings
  4. Guard the construction site with a null check and a clear message of your own

Example fix

// before
var settings = new RefitSettings(null, null, null, null); // throws

// after
var settings = new RefitSettings(
    new SystemTextJsonContentSerializer(),
    null, null, null);
Defensive patterns

Strategy: validation

Validate before calling

var serializer = ResolveSerializer()
    ?? throw new InvalidOperationException("No IHttpContentSerializer configured.");
var settings = new RefitSettings(serializer, null, null, null);

IHttpContentSerializer? ResolveSerializer() => /* from config */ null;

Type guard

static bool HasContentSerializer(RefitSettings? s) => s?.ContentSerializer is not null;

Try / catch

try { var settings = new RefitSettings(maybeSerializer, null, null, null); }
catch (ArgumentNullException ex) when (ex.ParamName == "contentSerializer")
{
    // supply a default serializer and construct again
}

Prevention

When it happens

Trigger: Constructing new RefitSettings(contentSerializer: null, ...) explicitly, or passing a null resolved from config/DI. Also reached if a custom factory returns null for the serializer.

Common situations: Reading serializer configuration from an optional config section that returned null; DI registration that forgot to register the serializer; refactoring a constructor call and dropping the serializer argument; default(SystemTextJsonContentSerializer) misuse.

Related errors


AI-assisted analysis of reactiveui/refit@b455f65ecc (2026-08-13). Data as JSON: /api/errors/f45dc63570dc7be1. Report an issue: GitHub.