{"record":{"id":"f45dc63570dc7be1","repo":"reactiveui/refit","slug":"the-content-serializer-can-t-be-null","errorCode":null,"errorMessage":"The content serializer can't be null","messagePattern":"The content serializer can't be null","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Refit/RefitSettings.cs","lineNumber":75,"sourceCode":"        : this(contentSerializer, urlParameterFormatter, formUrlEncodedParameterFormatter, null)\n    {\n    }\n\n    /// <summary>Initializes a new instance of the <see cref=\"RefitSettings\"/> class.</summary>\n    /// <param name=\"contentSerializer\">The <see cref=\"IHttpContentSerializer\"/> instance to use.</param>\n    /// <param name=\"urlParameterFormatter\">The <see cref=\"IUrlParameterFormatter\"/> instance to use (defaults to <see cref=\"DefaultUrlParameterFormatter\"/>).</param>\n    /// <param name=\"formUrlEncodedParameterFormatter\">The <see cref=\"IFormUrlEncodedParameterFormatter\"/> instance to use (defaults to <see cref=\"DefaultFormUrlEncodedParameterFormatter\"/>).</param>\n    /// <param name=\"urlParameterKeyFormatter\">The <see cref=\"IUrlParameterKeyFormatter\"/> instance to use (defaults to <see cref=\"DefaultUrlParameterKeyFormatter\"/>).</param>\n    /// <exception cref=\"ArgumentNullException\"><paramref name=\"contentSerializer\"/> is <see langword=\"null\"/>.</exception>\n    public RefitSettings(\n        IHttpContentSerializer contentSerializer,\n        IUrlParameterFormatter? urlParameterFormatter,\n        IFormUrlEncodedParameterFormatter? formUrlEncodedParameterFormatter,\n        IUrlParameterKeyFormatter? urlParameterKeyFormatter)\n    {\n        ContentSerializer =\n            contentSerializer\n            ?? throw new ArgumentNullException(\n                nameof(contentSerializer),\n                \"The content serializer can't be null\");\n        UrlParameterFormatter = urlParameterFormatter ?? new DefaultUrlParameterFormatter();\n        FormUrlEncodedParameterFormatter =\n            formUrlEncodedParameterFormatter ?? new DefaultFormUrlEncodedParameterFormatter();\n        UrlParameterKeyFormatter =\n            urlParameterKeyFormatter ?? new DefaultUrlParameterKeyFormatter();\n        ExceptionFactory = new DefaultApiExceptionFactory(this).CreateAsync;\n        TransportExceptionFactory = DefaultTransportExceptionFactory();\n    }\n\n    /// <summary>Gets or sets a function to provide the Authorization header. Does not work if you supply an HttpClient instance.</summary>\n    public Func<\n        HttpRequestMessage,\n        CancellationToken,\n        ValueTask<string>\n    >? AuthorizationHeaderValueGetter\n    { get; set; }","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/RefitSettings.cs#L57-L93","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a concrete IHttpContentSerializer, e.g. new SystemTextJsonContentSerializer()","If loading it from config, provide a sensible default when the config value is absent","Use a non-null DI registration for IHttpContentSerializer before resolving RefitSettings","Guard the construction site with a null check and a clear message of your own"],"exampleFix":"// before\nvar settings = new RefitSettings(null, null, null, null); // throws\n\n// after\nvar settings = new RefitSettings(\n    new SystemTextJsonContentSerializer(),\n    null, null, null);","handlingStrategy":"validation","validationCode":"var serializer = ResolveSerializer()\n    ?? throw new InvalidOperationException(\"No IHttpContentSerializer configured.\");\nvar settings = new RefitSettings(serializer, null, null, null);\n\nIHttpContentSerializer? ResolveSerializer() => /* from config */ null;","typeGuard":"static bool HasContentSerializer(RefitSettings? s) => s?.ContentSerializer is not null;","tryCatchPattern":"try { var settings = new RefitSettings(maybeSerializer, null, null, null); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"contentSerializer\")\n{\n    // supply a default serializer and construct again\n}","preventionTips":["Register IHttpContentSerializer in DI so it can never resolve to null","Default to new SystemTextJsonContentSerializer() when config does not specify one","Guard construction behind a factory that asserts the serializer is non-null","Add a startup check that RefitSettings.ContentSerializer is set"],"tags":["configuration","content-serializer","argument-null","constructor","argumentnullexception"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}