{"record":{"id":"b89e139610cb1574","repo":"restsharp/RestSharp","slug":"baseurl-must-be-set-when-using-a-client-factory","errorCode":null,"errorMessage":"BaseUrl must be set when using a client factory","messagePattern":"BaseUrl must be set when using a client factory","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/RestClient.cs","lineNumber":73,"sourceCode":"\n    /// <inheritdoc/>\n    public DefaultParameters DefaultParameters { get; }\n\n    /// <summary>\n    /// Creates an instance of RestClient using the provided <see cref=\"RestClientOptions\"/>\n    /// </summary>\n    /// <param name=\"options\">Client options</param>\n    /// <param name=\"configureDefaultHeaders\">Delegate to add default headers to the wrapped HttpClient instance</param>\n    /// <param name=\"configureSerialization\">Delegate to configure serialization</param>\n    /// <param name=\"useClientFactory\">Set to true if you wish to reuse the <seealso cref=\"HttpClient\"/> instance</param>\n    public RestClient(\n        RestClientOptions       options,\n        ConfigureHeaders?       configureDefaultHeaders = null,\n        ConfigureSerialization? configureSerialization  = null,\n        bool                    useClientFactory        = false\n    ) {\n        if (useClientFactory && options.BaseUrl == null) {\n            throw new ArgumentException(\"BaseUrl must be set when using a client factory\");\n        }\n\n        ConfigureSerializers(configureSerialization);\n        Options           = new(options);\n        DefaultParameters = new(Options);\n\n        if (useClientFactory) {\n            _disposeHttpClient = false;\n            HttpClient         = SimpleClientFactory.GetClient(options.BaseUrl!, GetClient);\n        }\n        else {\n            _disposeHttpClient = true;\n            HttpClient         = GetClient();\n        }\n\n        return;\n\n        HttpClient GetClient() {","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/RestClient.cs#L55-L91","documentation":"The RestClient options constructor throws ArgumentException when useClientFactory is true but options.BaseUrl is null. The SimpleClientFactory keys shared HttpClient instances by BaseUrl, so a null BaseUrl makes client reuse impossible and ambiguous.","triggerScenarios":"Calling new RestClient(options, ..., useClientFactory: true) where options.BaseUrl is null, or using the delegate overload new RestClient(configureRestClient, ..., useClientFactory: true) without setting BaseUrl in the delegate.","commonSituations":"Adopting the client-factory mode for HttpClient reuse/perf but forgetting to set BaseUrl; setting BaseUrl on the inner HttpClient instead of RestClientOptions.","solutions":["Set options.BaseUrl to a non-null Uri before passing useClientFactory: true.","Use the RestClient(Uri baseUrl, ..., useClientFactory: true) constructor overload which sets BaseUrl for you.","If you cannot provide a BaseUrl, do not enable useClientFactory (it defaults to false)."],"exampleFix":"// before\nvar options = new RestClientOptions { /* no BaseUrl */ };\nvar client = new RestClient(options, useClientFactory: true); // throws\n\n// after\nvar options = new RestClientOptions { BaseUrl = new Uri(\"https://api.example.com\") };\nvar client = new RestClient(options, useClientFactory: true);","handlingStrategy":"validation","validationCode":"if (useClientFactory && options.BaseUrl is null)\n    throw new InvalidOperationException(\"Set options.BaseUrl before enabling useClientFactory\");\nvar client = new RestClient(options, useClientFactory: useClientFactory);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set BaseUrl when useClientFactory is true.","Prefer the RestClient(Uri baseUrl, ..., useClientFactory: true) overload.","Validate BaseUrl in test fixtures that exercise the factory path."],"tags":["configuration","base-url","httpclient","client-factory","argument"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}