HttpClientInstaller.cs 1.24 KB
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Rcs.Application.Shared;
using Rcs.Cyaninetech.BackgroundServices;
using Rcs.Cyaninetech.Services;
using Rcs.Infrastructure.Shared;

namespace Rcs.Infrastructure.Installs;

/// <summary>
/// LanYin外挂服务安装器
/// </summary>
public static class HttpClientInstaller
{
    /// <summary>
    /// 安装HttpClient服务
    /// </summary>
    public static IServiceCollection InstallHttpClient(
        this IServiceCollection services)
    {
        // 注册通用HTTP客户端服务(如果尚未注册)
        if (!services.Any(x => x.ServiceType == typeof(IHttpClientService)))
        {
            services.AddHttpClient<IHttpClientService, HttpClientService>()
                .ConfigureHttpClient(client =>
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                })
                .ConfigurePrimaryHttpMessageHandler(() =>
                {
                    return new HttpClientHandler
                    {
                        ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
                    };
                });
        }
        
        return services;
    }
}