/// <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);
        }
Beispiel #2
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 FrameException($"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 FrameException($"方法特性不存在");
                }
            }
        }