当你的才华不能撑起你的野心时,就是你该选择学习的时候了!

.net core webapi Startup 注入ConfigurePrimaryHttpMessageHandler

asp.net core IHttpClientFactroy 进行SSL(https)请求时注入ConfigurePrimaryHttpMessageHandler解决https请求错误:
static public class CreditScoreServiceExtension
    {
        static public void AddCreditScoreQueryServiceHttpClient(this IServiceCollection services, IConfiguration config)
        {
            services.AddSingleton<ICreditScoreQueryService, CreditScoreQueryService>();

            //var serviceUrl = config.GetSection("AppSettings").GetSection("FC").GetSection("CreditScore")["ServiceUrl"];

            //var baseUri = new Uri(serviceUrl);

            services.AddHttpClient<ICreditScoreQueryService, CreditScoreQueryService>(client => { })
            .ConfigurePrimaryHttpMessageHandler(() =>
            {
                var handler = new HttpClientHandler();
                //if (baseUri.Scheme.ToLower() == "https")
                //{
                //    handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
                //}

                handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;

                return handler;
            }).SetHandlerLifetime(TimeSpan.FromMinutes(5));
        }
}

 

posted @ 2023-11-08 10:27  hofmann  阅读(35)  评论(0)    收藏  举报