Exemplo n.º 1
0
        /// <summary> 创建客户端 </summary>
        /// <param name="serviceAddress"></param>
        /// <returns></returns>
        protected override Task <IMicroClient> Create(ServiceAddress serviceAddress)
        {
            Logger.LogDebug($"创建客户端:{serviceAddress}创建客户端。");
            var          listener = new MessageListener();
            var          sender   = new HttpClientMessageSender(Provider, serviceAddress, listener);
            IMicroClient client   = new MicroClient(sender, listener, MicroExecutor, LoggerFactory);

            return(Task.FromResult(client));
        }
Exemplo n.º 2
0
        /// <summary> 创建客户端 </summary>
        /// <param name="serviceAddress"></param>
        /// <returns></returns>
        public Task <IMicroClient> CreateClient(ServiceAddress serviceAddress)
        {
            var lazyClient = _clients.GetOrAdd(serviceAddress, k => new Lazy <Task <IMicroClient> >(() =>
            {
                _logger.LogDebug($"创建客户端:{serviceAddress}创建客户端。");
                var listener = new MessageListener();
                var url      = serviceAddress.ToString();
                var sender   =
                    new HttpClientMessageSender(_loggerFactory, _clientFactory, _codecFactory, url, listener);
                IMicroClient client = new MicroClient(sender, listener, _microExecutor, _loggerFactory);
                return(Task.FromResult(client));
            }
                                                                                                    ));

            return(lazyClient.Value);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取包含MicroClient特性的程序集
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <returns></returns>
        public static IList <Type> GetMicroClientTypesByAssembly(string assemblyName)
        {
            List <Type> list      = new List <Type>();
            var         assembly  = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));
            var         typeinfos = assembly.DefinedTypes;

            foreach (var typeinfo in typeinfos)
            {
                // 判断是否有特性
                MicroClient microClient = typeinfo.GetCustomAttribute <MicroClient>();
                if (microClient != null)
                {
                    list.Add(typeinfo.AsType());
                }
            }
            return(list);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 客户端代理执行
        /// </summary>
        /// <param name="invocation"></param>
        public void Intercept(IInvocation invocation)
        {
            // 1、获取接口方法
            MethodInfo methodInfo = invocation.Method;

            // 2、获取方法上特性
            IEnumerable <Attribute> attributes = methodInfo.GetCustomAttributes();

            // 3、遍历
            foreach (var attribute in attributes)
            {
                // 1、获取Url
                Type        type        = invocation.Method.DeclaringType;
                MicroClient microClient = type.GetCustomAttribute <MicroClient>();
                if (microClient == null)
                {
                    throw new FrameworkException($"MicroClient 特性不能为空");
                }

                // 2、转换成动态参数
                ProxyMethodParameter proxyMethodParameter = new ProxyMethodParameter(methodInfo.GetParameters(), invocation.Arguments);
                dynamic paramPairs = ArgumentsConvert(proxyMethodParameter);

                if (attribute is GetPath getPath)
                {
                    // 3、路径变量替换
                    string path = PathParse(getPath.Path, paramPairs);

                    // 4、Get请求
                    dynamic result = middleService.GetDynamic(microClient.UrlShcme, microClient.ServiceName, path, paramPairs);

                    // 5、获取返回值类型
                    Type returnType = methodInfo.ReturnType;

                    // 8、赋值给返回值
                    invocation.ReturnValue = ResultConvert(result, returnType);
                }
                else if (attribute is PostPath postPath)
                {
                    // 3、路径变量替换
                    string path = PathParse(postPath.Path, paramPairs);

                    // 4、执行
                    dynamic result = middleService.PostDynamic(microClient.UrlShcme, microClient.ServiceName, path, paramPairs);

                    // 5、获取返回值类型
                    Type returnType = methodInfo.ReturnType;

                    // 8、赋值给返回值
                    invocation.ReturnValue = ResultConvert(result, returnType);
                }
                //else if (attribute is PutPath putPath)
                //{
                //    // 3、路径变量替换
                //    string path = PathParse(putPath.Path, paramPairs);

                //    // 4、执行
                //    dynamic result = middleService.PutDynamic(microClient.UrlShcme, microClient.ServiceName, path, paramPairs);

                //    // 5、获取返回值类型
                //    Type returnType = methodInfo.ReturnType;

                //    // 8、赋值给返回值
                //    invocation.ReturnValue = ResultConvert(result, returnType);
                //}
                //else if (attribute is DeletePath deletePath)
                //{
                //    // 3、路径变量替换
                //    string path = PathParse(deletePath.Path, paramPairs);

                //    // 4、执行
                //    dynamic result = middleService.DeleteDynamic(microClient.UrlShcme, microClient.ServiceName, path, paramPairs);

                //    // 5、获取返回值类型
                //    Type returnType = methodInfo.ReturnType;

                //    // 8、赋值给返回值
                //    invocation.ReturnValue = ResultConvert(result, returnType);
                //}
                else
                {
                    throw new FrameworkException($"方法特性不存在");
                }
            }
        }