コード例 #1
0
        /// <summary>
        /// 注册http接口
        /// </summary>
        /// <typeparam name="TInterface"></typeparam>
        /// <param name="configAction">HttpApiConfig的配置</param>
        /// <param name="handlerFunc">HttpMessageHandler创建委托</param>
        /// <returns></returns>
        public static bool Add <TInterface>(Action <HttpApiConfig> configAction, Func <HttpMessageHandler> handlerFunc) where TInterface : class, IHttpApi
        {
            lock (syncRoot)
            {
                var apiType = typeof(TInterface);
                if (factories.ContainsKey(apiType) == true)
                {
                    return(false);
                }

                var factory = new HttpApiFactory <TInterface>(configAction, handlerFunc);
                return(factories.TryAdd(apiType, factory));
            }
        }
コード例 #2
0
        /// <summary>
        /// 创建并记录指定接口的HttpApiFactory
        /// </summary>
        /// <typeparam name="TInterface"></typeparam>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public static HttpApiFactory <TInterface> Add <TInterface>() where TInterface : class, IHttpApi
        {
            lock (syncRoot)
            {
                var apiType = typeof(TInterface);
                if (factories.ContainsKey(apiType) == true)
                {
                    throw new InvalidOperationException($"不允许重复注册接口:{apiType}");
                }

                var factory = new HttpApiFactory <TInterface>();
                factories.TryAdd(apiType, factory);
                return(factory);
            }
        }
コード例 #3
0
        /// <summary>
        /// 创建并返回指定接口的HttpApiFactory
        /// </summary>
        /// <typeparam name="TInterface"></typeparam>
        /// <param name="name">工厂名称</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public static HttpApiFactory <TInterface> Add <TInterface>(string name) where TInterface : class, IHttpApi
        {
            if (string.IsNullOrEmpty(name) == true)
            {
                throw new ArgumentNullException(nameof(name));
            }

            lock (syncRoot)
            {
                if (factories.ContainsKey(name) == true)
                {
                    throw new InvalidOperationException($"不允许创建重复名称的HttpApiFactory:{name}");
                }

                var factory = new HttpApiFactory <TInterface>();
                factories.TryAdd(name, factory);
                return(factory);
            }
        }
コード例 #4
0
        /// <summary>
        /// 注册指定Api以及其工厂
        /// 返回Api工厂实例
        /// </summary>
        /// <param name="name">工厂名称</param>
        /// <param name="interfaceType">api接口类型</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public static HttpApiFactory Register(string name, Type interfaceType)
        {
            var factory = new HttpApiFactory(interfaceType);

            return(Register(name, factory));
        }
コード例 #5
0
        /// <summary>
        /// 注册指定Api以及其工厂
        /// 返回Api工厂实例
        /// </summary>
        /// <typeparam name="TInterface"></typeparam>
        /// <param name="name">工厂名称</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public static HttpApiFactory <TInterface> Register <TInterface>(string name) where TInterface : class, IHttpApi
        {
            var factory = new HttpApiFactory <TInterface>();

            return(Register(name, factory));
        }
コード例 #6
0
        /// <summary>
        /// 添加指定接口的HttpApiFactory
        /// </summary>
        /// <typeparam name="TInterface"></typeparam>
        /// <param name="factory">工厂实例</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public static HttpApiFactory <TInterface> Add <TInterface>(HttpApiFactory <TInterface> factory) where TInterface : class, IHttpApi
        {
            var name = GetFactoryName <TInterface>();

            return(Add(name, factory));
        }
コード例 #7
0
        /// <summary>
        /// 注册指定Api以及其工厂
        /// 返回Api工厂实例
        /// </summary>
        /// <typeparam name="TInterface"></typeparam>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public static HttpApiFactory <TInterface> Register <TInterface>() where TInterface : class, IHttpApi
        {
            var factory = new HttpApiFactory <TInterface>();

            return(RegisterFactory(factory));
        }