{"record":{"id":"7ae8eee0da4014bb","repo":"reactiveui/refit","slug":"baseaddress-must-be-set-on-the-httpclient-instance-7ae8ee","errorCode":null,"errorMessage":"BaseAddress must be set on the HttpClient instance","messagePattern":"BaseAddress must be set on the HttpClient instance","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Refit/RequestExecutionHelpers.cs","lineNumber":25,"sourceCode":"internal static partial class RequestExecutionHelpers\n{\n    /// <summary>The message used when content cannot be deserialized into the requested type.</summary>\n    private const string DeserializationErrorMessage = \"An error occured deserializing the response.\";\n\n    /// <summary>The error message used when the HTTP client has no base address configured.</summary>\n    private const string BaseAddressRequiredMessage = \"BaseAddress must be set on the HttpClient instance\";\n\n    /// <summary>Throws when a client cannot build relative generated requests.</summary>\n    /// <param name=\"client\">The HTTP client to inspect.</param>\n    /// <exception cref=\"InvalidOperationException\">Thrown when no base address is configured.</exception>\n    internal static void ThrowIfBaseAddressMissing(HttpClient client)\n    {\n        if (client.BaseAddress is not null)\n        {\n            return;\n        }\n\n        throw new InvalidOperationException(BaseAddressRequiredMessage);\n    }\n\n    /// <summary>Builds the token a request runs against, applying a per-call timeout on top of the effective token.</summary>\n    /// <param name=\"timeoutMilliseconds\">The per-call timeout in milliseconds; values that are not positive disable it.</param>\n    /// <param name=\"cancellationToken\">The already-resolved effective cancellation token for the request.</param>\n    /// <returns>The token the request should run against, and the timeout source to dispose once the request completes\n    /// (null when no timeout was applied).</returns>\n    internal static (CancellationToken Token, CancellationTokenSource? TimeoutSource) CreateTimeoutToken(\n        int timeoutMilliseconds,\n        CancellationToken cancellationToken)\n    {\n        if (timeoutMilliseconds <= 0)\n        {\n            return (cancellationToken, null);\n        }\n\n        var timeoutSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);\n        timeoutSource.CancelAfter(timeoutMilliseconds);","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/RequestExecutionHelpers.cs#L7-L43","documentation":"ThrowIfBaseAddressMissing throws InvalidOperationException when HttpClient.BaseAddress is null, guarding the reflection/execution path that needs a base address to build relative requests. It is the shared helper equivalent of the base-address checks inside BuildRelativeUri.","triggerScenarios":"Any execution path that calls ThrowIfBaseAddressMissing on a client with no BaseAddress: generated or reflection runners that build and send relative requests without a configured host.","commonSituations":"DI-registered HttpClient without ConfigureHttpClient setting BaseAddress; manual new HttpClient() passed to the client factory; integration tests that forget the host; switching from a host-based RestService.For(hostUrl) overload to a client-based one without setting the address.","solutions":["Set client.BaseAddress before use: client.BaseAddress = new Uri(\"https://api.example.com/\")","Prefer RestService.For<T>(hostUrl) or CreateHttpClient(hostUrl, settings) which set BaseAddress for you","In DI, configure via services.AddRefitClient<T>().ConfigureHttpClient(c => c.BaseAddress = new Uri(...))","Add a startup assertion that BaseAddress is non-null for clients handed to Refit"],"exampleFix":"// before\nvar client = new HttpClient();\nvar api = RestService.For<IHttpApi>(client);\nawait api.GetAsync(); // throws - base address missing\n\n// after\nvar client = new HttpClient { BaseAddress = new Uri(\"https://api.example.com/\") };\nvar api = RestService.For<IHttpApi>(client);\nawait api.GetAsync();","handlingStrategy":"validation","validationCode":"if (client.BaseAddress is null)\n    throw new InvalidOperationException(\n        \"HttpClient.BaseAddress must be set before creating a Refit client.\");\nvar api = RestService.For<IHttpApi>(client);","typeGuard":"static bool HasBaseAddress(HttpClient client) => client.BaseAddress is not null;","tryCatchPattern":"try { await api.GetAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"BaseAddress must be set\"))\n{\n    // reconfigure the client with a BaseAddress and retry\n}","preventionTips":["Create clients through RestService.For<T>(hostUrl) or CreateHttpClient(hostUrl, settings)","Configure BaseAddress in DI via ConfigureHttpClient on AddRefitClient","Assert BaseAddress is non-null in a startup health check","Centralize client construction to avoid bare new HttpClient() reaching Refit"],"tags":["configuration","http-client","base-address","invalidoperation"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}