【发布时间】:2023-02-24 16:31:27
【问题描述】:
我有一个用于记录目的的自定义委托处理程序:
public class LoggingDelegatingHandler : DelegatingHandler {
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
//my logging
return await base.SendAsync(request, cancellationToken);
}
}
我一直在尝试像这样在我的依赖注入中注入它:
services.AddHttpClient(nameof(MyAuthorityClient),
c => { c.BaseAddress = new Uri(myOptions.BaseUri); })
.AddTransientHttpErrorPolicy(x =>
x.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(retryAttempt)))
.ConfigurePrimaryHttpMessageHandler<MyClientHttpHandler>()
.AddHttpMessageHandler<LoggingDelegatingHandler>();
好像可以编译。但是当执行命中这段代码时:
ar response = await client.GetAsync("/my/api/path");
调试器永远不会到达我的LoggingDelegatingHandler 中的SendAsync 方法。
起初,我以为是因为我正在调用GetAsync,而我的重写方法是SendAsync,但后来我读到它应该仍然命中SendAsync。
我究竟做错了什么?
【问题讨论】: