restsharp/RestSharp · error · ArgumentException

BaseUrl must be set when using a client factory

Error message

BaseUrl must be set when using a client factory

What it means

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.

Source

Thrown at src/RestSharp/RestClient.cs:73

    /// <inheritdoc/>
    public DefaultParameters DefaultParameters { get; }

    /// <summary>
    /// Creates an instance of RestClient using the provided <see cref="RestClientOptions"/>
    /// </summary>
    /// <param name="options">Client options</param>
    /// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
    /// <param name="configureSerialization">Delegate to configure serialization</param>
    /// <param name="useClientFactory">Set to true if you wish to reuse the <seealso cref="HttpClient"/> instance</param>
    public RestClient(
        RestClientOptions       options,
        ConfigureHeaders?       configureDefaultHeaders = null,
        ConfigureSerialization? configureSerialization  = null,
        bool                    useClientFactory        = false
    ) {
        if (useClientFactory && options.BaseUrl == null) {
            throw new ArgumentException("BaseUrl must be set when using a client factory");
        }

        ConfigureSerializers(configureSerialization);
        Options           = new(options);
        DefaultParameters = new(Options);

        if (useClientFactory) {
            _disposeHttpClient = false;
            HttpClient         = SimpleClientFactory.GetClient(options.BaseUrl!, GetClient);
        }
        else {
            _disposeHttpClient = true;
            HttpClient         = GetClient();
        }

        return;

        HttpClient GetClient() {

View on GitHub (pinned to 6a50821692)

Solutions

  1. Set options.BaseUrl to a non-null Uri before passing useClientFactory: true.
  2. Use the RestClient(Uri baseUrl, ..., useClientFactory: true) constructor overload which sets BaseUrl for you.
  3. If you cannot provide a BaseUrl, do not enable useClientFactory (it defaults to false).

Example fix

// before
var options = new RestClientOptions { /* no BaseUrl */ };
var client = new RestClient(options, useClientFactory: true); // throws

// after
var options = new RestClientOptions { BaseUrl = new Uri("https://api.example.com") };
var client = new RestClient(options, useClientFactory: true);
Defensive patterns

Strategy: validation

Validate before calling

if (useClientFactory && options.BaseUrl is null)
    throw new InvalidOperationException("Set options.BaseUrl before enabling useClientFactory");
var client = new RestClient(options, useClientFactory: useClientFactory);

Prevention

When it happens

Trigger: 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.

Common situations: Adopting the client-factory mode for HttpClient reuse/perf but forgetting to set BaseUrl; setting BaseUrl on the inner HttpClient instead of RestClientOptions.

Related errors


AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13). Data as JSON: /api/errors/b89e139610cb1574. Report an issue: GitHub.