reactiveui/refit · error · ArgumentException

`hostUrl` must not be null or whitespace.

Error message

`hostUrl` must not be null or whitespace.

What it means

RestService.CreateHttpClient throws ArgumentException when hostUrl is null, empty, or whitespace, because it becomes the HttpClient.BaseAddress and an empty base address is unusable. On .NET 8+ it delegates to ArgumentException.ThrowIfNullOrWhiteSpace; otherwise it checks manually and throws with the parameter name.

Source

Thrown at src/Refit/RestService.cs:398

            DynamicallyAccessedMemberTypes.Interfaces
            | DynamicallyAccessedMemberTypes.PublicMethods
            | DynamicallyAccessedMemberTypes.NonPublicMethods)]
        Type refitInterfaceType,
        string hostUrl) => For(refitInterfaceType, hostUrl, null);

    /// <summary>Create an <see cref="HttpClient"/> with <paramref name="hostUrl"/> as the base address.</summary>
    /// <param name="hostUrl">Base address.</param>
    /// <param name="settings"><see cref="RefitSettings"/> to use to configure the HttpClient.</param>
    /// <returns>A <see cref="HttpClient"/> with the various parameters provided.</returns>
    /// <exception cref="ArgumentException">Thrown when <paramref name="hostUrl"/> is null or whitespace.</exception>
    public static HttpClient CreateHttpClient(string hostUrl, RefitSettings? settings)
    {
#if NET8_0_OR_GREATER
        ArgumentException.ThrowIfNullOrWhiteSpace(hostUrl);
#else
        if (string.IsNullOrWhiteSpace(hostUrl))
        {
            throw new ArgumentException(
                $"`{nameof(hostUrl)}` must not be null or whitespace.",
                nameof(hostUrl));
        }
#endif

        // check to see if user provided custom auth token
        HttpMessageHandler? innerHandler = null;
        if (settings is not null)
        {
            if (settings.HttpMessageHandlerFactory is not null)
            {
                innerHandler = settings.HttpMessageHandlerFactory();
            }

            if (settings.AuthorizationHeaderValueGetter is not null)
            {
                innerHandler = new AuthenticatedHttpClientHandler(
                    settings.AuthorizationHeaderValueGetter,

View on GitHub (pinned to b455f65ecc)

Solutions

  1. Pass a non-empty absolute URL string, e.g. "https://api.example.com"
  2. Validate the host at startup and fail fast with a clear message if config is missing
  3. Provide a fallback/default URL via the null-coalescing operator when reading config
  4. Bind the URL through IOptions<ApiOptions> with data annotations validation

Example fix

// before
var host = config["Api:BaseUrl"]; // null if missing
var client = RestService.CreateHttpClient(host, settings); // throws

// after
var host = config["Api:BaseUrl"]
    ?? throw new InvalidOperationException("Api:BaseUrl is not configured");
var client = RestService.CreateHttpClient(host, settings);
Defensive patterns

Strategy: validation

Validate before calling

var host = configuration["Api:BaseUrl"];
if (string.IsNullOrWhiteSpace(host))
    throw new InvalidOperationException("Api:BaseUrl is not configured.");
var client = RestService.CreateHttpClient(host, settings);

Type guard

static bool IsValidHost(string? host) =>
    !string.IsNullOrWhiteSpace(host);

Try / catch

try { var client = RestService.CreateHttpClient(host, settings); }
catch (ArgumentException ex) when (ex.ParamName == nameof(host))
{
    // surface a clear configuration error to the operator
}

Prevention

When it happens

Trigger: Calling RestService.CreateHttpClient(null, settings), RestService.For<T>(""), or passing a hostUrl loaded from a missing config key that resolves to null or empty.

Common situations: Reading the API base URL from IConfiguration/IOptions and the section being absent; environment variable not set in production; typo in the config key; default(string) passed through a helper.

Related errors


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