一键导入
dck-httpclient-factory
Use when configuring AHKFlowApp HttpClient, IHttpClientFactory, typed clients, handlers, retries, resilience, or external API calls.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when configuring AHKFlowApp HttpClient, IHttpClientFactory, typed clients, handlers, retries, resilience, or external API calls.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use when creating, removing, or troubleshooting AHKFlowApp git worktrees across Claude Code, Codex, or Copilot.
Use when optimizing AHKFlowApp agent workflow, worktrees, planning, verification loops, context budget, or tool usage.
Use when verifying AHKFlowApp build, tests, formatting, diagnostics, security, or readiness before commit, push, or PR.
Compact the current conversation into a handoff document for another agent to pick up.
Use to scan .NET code for performance anti-patterns across async, memory, strings, collections, LINQ, regex, and I/O.
Use to evaluate assertion depth and diversity across a test suite and flag assertion-free, shallow, or tautological tests.
| name | dck-httpclient-factory |
| description | Use when configuring AHKFlowApp HttpClient, IHttpClientFactory, typed clients, handlers, retries, resilience, or external API calls. |
IHttpClientFactory or DI-registered clients instead of new HttpClient()..AddStandardResilienceHandler() to all outbound HTTP clients.DelegatingHandlers.builder.Services.AddHttpClient("github", client =>
{
client.BaseAddress = new Uri("https://api.github.com/");
client.DefaultRequestHeaders.UserAgent.ParseAdd("AHKFlowApp/1.0");
})
.AddStandardResilienceHandler();
internal sealed class GitHubReleaseClient(IHttpClientFactory httpClientFactory)
{
public async Task<ReleaseDto?> GetLatestAsync(CancellationToken cancellationToken)
{
var client = httpClientFactory.CreateClient("github");
return await client.GetFromJsonAsync<ReleaseDto>(
"repos/owner/repo/releases/latest",
cancellationToken);
}
}
builder.Services.AddHttpClient("payments", client =>
{
client.BaseAddress = new Uri("https://api.payments.example/");
})
.AddStandardResilienceHandler()
.AddAsKeyed();
[ApiController]
[Route("api/v1/[controller]")]
[Authorize]
public sealed class PaymentsController(
[FromKeyedServices("payments")] HttpClient paymentsClient)
: ControllerBase
{
[HttpPost("charge")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status502BadGateway)]
public async Task<IActionResult> Charge(ChargeRequest request, CancellationToken cancellationToken)
{
using var response = await paymentsClient.PostAsJsonAsync("charges", request, cancellationToken);
return response.IsSuccessStatusCode ? NoContent() : StatusCode(StatusCodes.Status502BadGateway);
}
}
Prefer use-case handlers for business workflows; controller examples only show DI shape.
AddStandardResilienceHandler() provides total timeout, attempt timeout, retry, circuit breaker, and rate limiting defaults.
builder.Services.AddHttpClient("external-api")
.AddStandardResilienceHandler(options =>
{
options.Retry.MaxRetryAttempts = 3;
options.Retry.DisableForUnsafeHttpMethods();
options.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(30);
});
public sealed class CorrelationIdHandler(IHttpContextAccessor accessor) : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (accessor.HttpContext?.TraceIdentifier is { Length: > 0 } traceId)
{
request.Headers.TryAddWithoutValidation("X-Correlation-Id", traceId);
}
return base.SendAsync(request, cancellationToken);
}
}
builder.Services.AddTransient<CorrelationIdHandler>();
builder.Services.AddHttpClient("external-api")
.AddHttpMessageHandler<CorrelationIdHandler>()
.AddStandardResilienceHandler();
Use a fake HttpMessageHandler for code that accepts an HttpClient, or register a test client in DI. Do not mock framework-owned HttpClient methods.
public sealed class StubHttpMessageHandler(HttpResponseMessage response) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken) =>
Task.FromResult(response);
}
using var client = new HttpClient() inside request paths..AddStandardResilienceHandler().DefaultRequestHeaders.Authorization per request on a shared client.CancellationToken.| Scenario | Pattern |
|---|---|
| One external API | Named or keyed client |
| Auth header | DelegatingHandler |
| Per-request correlation | DelegatingHandler |
| Singleton service | IHttpClientFactory or correctly scoped keyed client |
| Non-idempotent POST | Standard resilience with unsafe retries disabled |