我想我需要弄清楚如何从服务引用中获取httpclient。
我一直在连接到返回证书错误的Web服务。我想绕过该错误,在dotnet框架中,这很简单。但是,在内核中,我被告知必须获取httpClient并为其添加自定义处理程序。很好,但是当我有一个Web服务的服务引用对象时,我不知道如何从中获取httpClient。
首先,我向Web服务(不是我控制的一个)添加服务引用。
下一个,
BasicHttpBinding httpBinding =新的BasicHttpBinding();
httpBinding.Security.Mode = BasicHttpSecurityMode.Transport;
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
httpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
SRI_CAP_AlertCollector.AlertCollectorServicePortClient client = new SRI_CAP_AlertCollector.AlertCollectorServicePortClient(httpBinding, endpoint);
但是当我异步调用服务方法之一时,由于证书,我得到一个错误。由于这是一个概念证明,因此我想绕过证书错误。
在DotNetCore中,此操作与dotnet框架不同。所以我试图找出如何从服务引用中获取httpClientHandler,以便可以添加ServerCertificateCustomValidationCallback
因为您使用的是.net core,所以我建议您使用HttpClientFactory
services.AddHttpClient("signed")
.ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler();
//Get certificate
X509Certificate2 cert = null;
//Load your certificate
if (cert == null) throw new IndexOutOfRangeException($"No certificate found");
handler.ClientCertificates.Add(cert);
handler.ServerCertificateCustomValidationCallback += _validateCertificateCallback;
return handler;
});