简体   繁体   中英

ServerCertificateCustomValidationCallback is not invoked when using Polly

I'm using Polly to send https requests to an external server, the probleme is the ServerCertificateCustomValidationCallback is never invoked:

public class CustomeHttpClient : HttpClientHandler {
 ...
 ...
 protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {

  ServerCertificateCustomValidationCallback = (requestMessage, certificate, chain, sslError) =>
  {
    // custome validations
  };
   return await Policy
               .Handle<Exception>()
               .WaitAndRetryAsync(_retryPolicy.RetryCount,
                                  retryAttempt => TimeSpan.FromMilliseconds(_retryPolicy.MillisecondsBeforeRetry),
                                  (ex, t, retryCount, ctx) => _logger.LogWarning("Retry attempt SendAsync {OutgoingRequestUri}", request.RequestUri))
               .ExecuteAsync(async () => 
               {
                   return await base.SendAsync(request, cancellationToken);
               });
 }
}

the ServerCertificateCustomValidationCallback callback is invoked only when I send request without Polly,
Any idea how to solve this issue?
Thanks

I just tried to use your HttpClientHandler , works totally fine:

var t1 = Task.Run(async () =>
{
    var cl = new HttpClient(new CustomeHttpClient());
    var msg = new HttpRequestMessage(HttpMethod.Get, "https://www.google.com");

    var response = await cl.SendAsync(msg);
});

A Breakpoint inside your ServerCertificateCustomValidationCallback is hit as expected while using a Polly policy.

public class CustomeHttpClient : HttpClientHandler
{

     protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
     {

        ServerCertificateCustomValidationCallback = (requestMessage, certificate, chain, sslError) =>
        {
            // a breakpoint here is hit as expected
            return true;
        };

        return await Policy...

Maybe you never actually use your CustomeHttpClient ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM