/// <summary> /// 将 <see cref="IWechatApiService"/> 依赖项的默认实现注入到给定的依赖服务集中。 /// </summary> /// <param name="services">依赖服务集。</param> /// <returns></returns> /// <exception cref="ArgumentNullException"> /// <paramref name="services"/>为null /// 或 /// <paramref name="options"/>为null /// </exception> /// <exception cref="InvalidOperationException">用于访问微信 API 的 AppID 或 AppSecret 未配置。</exception> public static IServiceCollection AddDefaultWechatApiService(this IServiceCollection services, Action <WechatApiServiceOptions> options) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } var optionObject = new WechatApiServiceOptions(); options(optionObject); if (optionObject.AppId == null || optionObject.AppSecret == null) { throw new InvalidOperationException("用于访问微信 API 的 AppID 或 AppSecret 未配置。"); } return(services.AddSingleton <IWechatApiService, DefaultWechatApiService>( serviceProvider => new DefaultWechatApiService( optionObject, serviceProvider.GetService <IHttpClientFactory>(), serviceProvider.GetService <ILogger <DefaultWechatApiService> >()))); }
/// <summary> /// 初始化 <see cref="DefaultWechatApiService"/> 类的新实例。 /// </summary> /// <param name="options">服务配置对象。</param> /// <param name="httpClientFactory">HTTP客户端工厂对象。</param> /// <param name="logger"></param> /// <exception cref="ArgumentNullException"><paramref name="httpClientFactory"/>为null。</exception> public DefaultWechatApiService(WechatApiServiceOptions options, IHttpClientFactory httpClientFactory, ILogger <DefaultWechatApiService> logger) { _options = options ?? throw new ArgumentNullException(nameof(options)); _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); }