コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="serviceConfigure"></param>
        /// <param name="moduleName"></param>
        /// <param name="configAction"></param>
        /// <returns></returns>
        public static HttpServiceConfigureItem AddModule(this HttpServiceConfigureItem serviceConfigure, string moduleName, Action <HttpOption> configAction)
        {
            var option = new HttpOption();

            configAction?.Invoke(option);
            serviceConfigure.ModuleOptions[moduleName] = option;
            return(serviceConfigure);
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="services"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static HttpServiceConfigureItem AddHttpService(this IServiceCollection services, string name)
        {
            var configure = new HttpServiceConfigureItem
            {
                ServiceName = name
            };

            return(services.AddHttpService(configure));
        }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="services"></param>
        /// <param name="name"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static HttpServiceConfigureItem AddHttpService(this IServiceCollection services, string name, Action <HttpServiceConfigureItem> action)
        {
            var configure = new HttpServiceConfigureItem
            {
                ServiceName = name
            };

            action?.Invoke(configure);
            return(services.AddHttpService(configure));
        }
コード例 #4
0
        /// <summary>
        /// 批量注册 用于单个项目对接一个服务时
        /// </summary>
        /// <param name="serviceConfigure"></param>
        /// <param name="assembly"></param>
        /// <returns></returns>
        public static HttpServiceConfigureItem ForTypes(this HttpServiceConfigureItem serviceConfigure, Assembly assembly)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }
            var types = assembly.GetExportedTypes()
                        .Where(type => typeof(IHttpService).IsAssignableFrom(type) || type.GetCustomAttribute <HttpServiceAttribute>() != null);

            return(serviceConfigure.ForTypes(types.ToArray()));
        }
コード例 #5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="serviceConfigure"></param>
 /// <param name="types"></param>
 /// <returns></returns>
 public static HttpServiceConfigureItem ForTypes(this HttpServiceConfigureItem serviceConfigure, params Type[] types)
 {
     if (types == null || types.Length <= 0)
     {
         return(serviceConfigure);
     }
     foreach (var type in types)
     {
         serviceConfigure.ForTypes.Add(type);
     }
     return(serviceConfigure);
 }
コード例 #6
0
        /// <summary>
        /// 添加服务配置,并添加对应的httpClient
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configure"></param>
        /// <returns></returns>
        public static HttpServiceConfigureItem AddHttpService(this IServiceCollection services, HttpServiceConfigureItem configure)
        {
            var configuration = services.ServiceConfigure();

            var config = configuration.AddConfigure(configure);

            services.AddHttpClient(configure.ServiceName, client =>
            {
                //设置默认baseAddress
                if (!string.IsNullOrEmpty(config.BaseAddress))
                {
                    client.BaseAddress = new Uri(config.BaseAddress, UriKind.Absolute);
                }
                //设置默认headers
                if (config.Option.DefaultHeaders.Count > 0)
                {
                    foreach (var key in config.Option.DefaultHeaders.Keys)
                    {
                        client.DefaultRequestHeaders.Add(key, config.Option.DefaultHeaders[key]);
                    }
                }
                //设置请求超时时间(默认30秒)
                client.Timeout = TimeSpan.FromSeconds(config.Timeout > 0 ? config.Timeout : 30);
            });
            services.Replace(ServiceDescriptor.Singleton(configuration));
            return(config);
        }
コード例 #7
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="serviceConfigure"></param>
 /// <param name="moduleName"></param>
 /// <param name="option"></param>
 /// <returns></returns>
 public static HttpServiceConfigureItem AddModule(this HttpServiceConfigureItem serviceConfigure, string moduleName, HttpOption option)
 {
     serviceConfigure.ModuleOptions[moduleName] = option;
     return(serviceConfigure);
 }
コード例 #8
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="TService"></typeparam>
 /// <param name="serviceConfigure"></param>
 /// <returns></returns>
 public static HttpServiceConfigureItem For <TService>(this HttpServiceConfigureItem serviceConfigure)
 {
     serviceConfigure.ForTypes.Add(typeof(TService));
     return(serviceConfigure);
 }