Skip to content

HttpClientOptionsAttribute

NexusLabs.Needlr.Generators

HttpClientOptionsAttribute Class

Marks a class as a named HttpClient configuration type. The source generator will emit both an AddOptions<T>().BindConfiguration(...) call and a matching services.AddHttpClient(name, (sp, client) => { ... }) registration, so consumers never have to hand-write either one.

public sealed class HttpClientOptionsAttribute : System.Attribute

Inheritance System.Object 🡒 System.Attribute 🡒 HttpClientOptionsAttribute

Example

// Minimal form — name inferred as "WebFetch", section inferred as "HttpClients:WebFetch"
[HttpClientOptions]
public sealed record WebFetchHttpClientOptions : IStandardHttpClientOptions
{
    public string    ClientName     => "WebFetch"; // optional when suffix-stripping works
    public TimeSpan  Timeout        { get; init; } = TimeSpan.FromSeconds(15);
    public string?   UserAgent      { get; init; } = "BrandGhost-Agent/1.0";
    public Uri?      BaseAddress    { get; init; }
    public IReadOnlyDictionary<string, string>? DefaultHeaders { get; init; }
}

// Explicit section
[HttpClientOptions("Upstream:Tavily")]
public sealed record TavilyHttpClientOptions : IStandardHttpClientOptions { ... }

// Explicit name override
[HttpClientOptions(Name = "tavily-primary")]
public sealed record TavilyPrimaryHttpClientOptions : IStandardHttpClientOptions { ... }

Remarks

The decorated type MUST implement INamedHttpClientOptions. It opts into additional configurability by implementing capability interfaces such as IHttpClientTimeout, IHttpClientUserAgent, IHttpClientBaseAddress, and IHttpClientDefaultHeaders. The generator emits only the wiring for capabilities actually implemented, so there is no dead code and no attribute property churn when new capabilities are added.

The resolved HttpClient name comes from, in order of precedence: 1. The attribute's Name property, if set. 2. A ClientName property on the decorated type with a literal expression body (e.g. public string ClientName => "WebFetch";). 3. Inferred from the type name by stripping the suffixes HttpClientOptions, HttpClientSettings, or HttpClient.

The resolved configuration section comes from the attribute's constructor argument, or is inferred as "HttpClients:<ResolvedName>" when no section is given.

Constructors

HttpClientOptionsAttribute() Constructor

Initializes a new instance of the HttpClientOptionsAttribute class with the section name inferred from the resolved client name ("HttpClients:<ResolvedName>").

public HttpClientOptionsAttribute();

HttpClientOptionsAttribute(string) Constructor

Initializes a new instance of the HttpClientOptionsAttribute class with an explicit configuration section name.

public HttpClientOptionsAttribute(string sectionName);

Parameters

sectionName System.String

The configuration section to bind to (e.g., "HttpClients:WebFetch" or "Upstream:Tavily").

Properties

HttpClientOptionsAttribute.Name Property

Gets or sets the explicit HttpClient name. When set, this overrides both the ClientName property on the decorated type and the type-name inference fallback.

public string? Name { get; set; }

Property Value

System.String

HttpClientOptionsAttribute.SectionName Property

Gets the explicit configuration section name, or null to infer it from the resolved client name.

public string? SectionName { get; }

Property Value

System.String