public override object InvokeResult(object rq, Func <Type, object> objectCtor = null) { var request = rq as RequestMessage; var response = new ResponseMessage(); try { var a = serviceHandle.TryGetValue(request.Service, out serviceInfo serviceInfo); if (!a) { return(ResponseMessage.CreateError("未找到该服务", "404")); } var methodInfo = serviceInfo.GetMethod(request.Method); if (methodInfo == null) { return(ResponseMessage.CreateError("未找到该方法" + request.Method, "404")); } var method = methodInfo.MethodInfo; var paramters = request.Args; var methodParamters = methodInfo.Parameters; var args = new object[methodParamters.Length]; for (int i = 0; i < methodParamters.Length; i++) { var p = methodParamters[i]; var value = paramters[i]; int offSet = 0; args[i] = Core.BinaryFormat.FieldFormat.UnPack(p.ParameterType, value, ref offSet); } var msgBase = new Core.Remoting.MessageBase() { Args = args.ToList(), Method = request.Method, Service = request.Service, Token = request.Token }; var errorInfo = InvokeMessage(msgBase, out object result, out Dictionary <int, object> outs2, out string token, objectCtor); if (errorInfo != null) { return(ResponseMessage.CreateError(errorInfo.msg, errorInfo.code)); } var generType = method.ReturnType; if (methodInfo.IsAsync) { generType = method.ReturnType.GenericTypeArguments[0]; } response.SetData(generType, result); response.Success = true; var outs = new Dictionary <int, byte[]>(); foreach (var kv in outs2) { var type = methodParamters[kv.Key]; var value = kv.Value; outs[kv.Key] = Core.BinaryFormat.FieldFormat.Pack(type.ParameterType, value); } response.Outs = outs; if (!string.IsNullOrEmpty(token))//登录方法后返回新TOKEN { response.Token = token; } } catch (Exception ex) { response.Success = false; response.Msg = ex.Message; Console.WriteLine(ex.ToString()); return(ResponseMessage.CreateError(ex.Message + $" 在{request.Service}/{request.Method}", "500")); } return(response); }
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { try { if (channel == null || !channel.Open) { channel = Core.AsyncInvoke.RunSync(() => bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(HostAddress.address), HostAddress.port))); //channel = bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(HostAddress.address), HostAddress.port)).Result; } } catch (Exception ero) { ThrowError("连接服务端失败:" + ero, "500"); } var id = Guid.NewGuid().ToString(); var method = ServiceType.GetMethod(binder.Name); allWaits.Add(id); var request = new RequestMessage { MsgId = id, Service = ServiceName, Method = binder.Name, //Token = clientConnect.Token.Token }; var dic = new List <byte[]>(); var allArgs = method.GetParameters(); for (int i = 0; i < allArgs.Length; i++) { var p = allArgs[i]; dic.Add(Core.BinaryFormat.FieldFormat.Pack(p.ParameterType, args[i])); } request.Args = dic; var token = request.Token; request.Token = CreateAccessToken(allArgs, args.ToList(), clientConnect.TokenInfo); var pollyAttr = serviceInfo.GetAttribute <PollyAttribute>(); ResponseMessage response = null; var pollyData = PollyExtension.Invoke(pollyAttr, () => { channel.WriteAndFlushAsync(request.ToBuffer()); //等待返回 var res = allWaits.Wait(id).Response; return(new PollyExtension.PollyData <ResponseMessage>() { Data = res }); }, $"{ServiceName}.{method.Name}"); response = pollyData.Data; if (!string.IsNullOrEmpty(pollyData.Error)) { ThrowError(pollyData.Error, "500"); } if (response == null) { ThrowError("请求超时未响应", "500"); } if (!response.Success) { ThrowError($"服务端处理错误:{response.Msg}", response.GetData(typeof(string)) + ""); } var returnType = method.ReturnType; if (response.Outs != null && response.Outs.Count > 0) { foreach (var kv in response.Outs) { var index = kv.Key; var type = allArgs[index]; //args[(int)find] = kv.Value; int offSet = 0; args[index] = Core.BinaryFormat.FieldFormat.UnPack(type.ParameterType, kv.Value, ref offSet); } } if (!string.IsNullOrEmpty(response.Token)) { clientConnect.TokenInfo.Token = response.Token; } if (returnType == typeof(void)) { result = null; return(true); } var generType = returnType; bool isTask = false; if (returnType.Name.StartsWith("Task`1")) { generType = returnType.GenericTypeArguments[0]; isTask = true; } result = response.GetData(generType); if (isTask) { //返回Task类型 var method2 = typeof(Task).GetMethod("FromResult", BindingFlags.Public | BindingFlags.Static); var result2 = method2.MakeGenericMethod(new Type[] { generType }).Invoke(null, new object[] { result }); result = result2; } return(true); }