[英]Using factory method to create typed HTTP client, ASP.NET Core 2.1
[英]ASP.NET Core 2.1: Add client certificate to registered typed HTTP clients
我介绍了ASP.NET 2.1核心的新方式等中描述的在我的应用程序中创建/注册HTTP客户端在这里。 在我需要向键入的 HTTP 客户端添加客户端证书之前,它一直运行良好。
我做了很多研究,但我没有找到一种方法来做到这一点..
我期待这样的事情:
services.AddHttpClient<IMonitoringService, MonitoringService>(
client =>
{
client.BaseAddress = new Uri(this.Configuration.GetSection("ServiceEndpoints:Monitoring").Get<ServiceEndpointConfiguration>().Endpoint);
client.ClientCertificates.Add(new X509Certificate2(/* ... */)); // Error: `ClientCertificates` doesn't exists.
});
或者像这样:
services.AddHttpClient<IMonitoringService, MonitoringService>(
client => client.BaseAddress = new Uri(this.Configuration.GetSection("ServiceEndpoints:Monitoring").Get<ServiceEndpointConfiguration>().Endpoint))
.AddHttpMessageHandler(
() =>
{
var handler = new HttpClientHandler();
handler.ClientCertificates.Add(new X509Certificate2(/* ... */));
return handler; // Error: Expected return type is `DelegatingHandler`.
});
但是这样的方式不存在..
有没有办法做到这一点?
根据您的经验, AddHttpMessageHandler
需要返回DelegatingHandler
类型是正确的,因为此函数用于扩展请求管道。 为了创建一个附加了客户端证书的HttpClientHandler
,您应该改为使用ConfigurePrimaryHttpMessageHandler
来创建并返回HttpClientHandler
的配置实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.