5.15Setting the HttpClient Instance Name (Multiple Base Addresses)

Updated on Aug 20, 2026~3 min read

The system uses IHttpClientFactory to create HttpClient instances by default, and sets the default client name to an empty string (string.Empty). You can set the client name used when creating the HttpClient instance via the HttpClientNameAttribute attribute.

HTTP Declarative Requests set the HttpClient instance name via the HttpClientNameAttribute attribute. The corresponding HTTP declarative extractor is implemented as the HttpClientNameDeclarativeExtractor type, which is responsible for parsing the HttpClientNameAttribute attribute and building the HttpClient instance name configuration required by the HttpRequestBuilder instance.

cs
// Applied on the interface definition, affecting all methods[HttpClientName(string.Empty)]public interface IHttpService : IHttpDeclarative{    [Get("https://furion.net/")]    Task<string> GetStringAsync();  // Default client    // Applied on the method    [HttpClientName("weixin")]   // Specified as the client named "weixin"    [Get("https://furion.net/")]    Task<string> GetStringAsync();}

You can also provide configuration for named HttpClient clients in the Startup.cs or Program.cs file:

cs
// Configure the default client (with an empty string name)services.AddHttpClient(string.Empty, client => { });// Configure the client named "weixin"services.AddHttpClient("weixin", client => { });

HttpClientNameAttribute includes the following constructors and properties:

  • Constructors:

    • new(name): Applies to methods or interfaces, setting the HttpClient instance name.
  • Properties:

    • Name: The HttpClient instance name (string type).