public ITransportableObject SerializeInvokePacket(InvokePacket invokePacket) { JsonTransportable transportable = null; var jsonData = JsonConvert.SerializeObject(invokePacket, _Settings); transportable = new JsonTransportable(jsonData); return(transportable); }
public InvokePacket DeSerializeToInvokePacket(ITransportableObject tranObj) { InvokePacket packet = null; var jsonTranObj = tranObj as JsonTransportable; var jsonString = jsonTranObj.JsonString; if (jsonString == null) { return(packet); } JObject jObject = JObject.Parse(jsonString); JsonSerializer serializer = JsonSerializer.Create(_Settings); var jtoken = jObject.GetValue(nameof(InvokePacket.ParameterValues)); //若有传入参数 if (jtoken != null) { jObject.Remove(nameof(InvokePacket.ParameterValues)); packet = jObject.ToObject <InvokePacket>(serializer); Type[] parameterTypes = new Type[packet.ParameterTypes.Length]; Object[] parameterValues = new Object[packet.ParameterTypes.Length]; for (Int32 i = 0; i < packet.ParameterTypes.Length; ++i) { parameterTypes[i] = Utilities.GetType(packet.ParameterTypes[i], true); } var jarrayObject = jtoken as JArray; if (packet.ParameterTypes.Length == 1) { if (parameterTypes[0].IsArray) { parameterValues[0] = jarrayObject.ToObject(parameterTypes[0], serializer); } else { parameterValues[0] = jarrayObject[0].ToObject(parameterTypes[0], serializer); } } else { for (Int32 i = 0; i < parameterTypes.Length; ++i) { var jtokenObject = jarrayObject[i]; parameterValues[i] = jtokenObject.ToObject(parameterTypes[i], serializer); } } packet.ParameterValues = parameterValues; } else { packet = jObject.ToObject <InvokePacket>(serializer); } return(packet); }
public Object InovkeHandle(Object proxy, Type interfaceType, MethodInfo invokeMethod, Object[] parameterValues) { Object result = null; try { InvokeOperationInfo invokeInfo = InvokeOperationInfo.Create(interfaceType, invokeMethod); InvokePacket invokePacket = new InvokePacket(); invokePacket.InterfaceName = invokeInfo.InterfaceSchema.Name; invokePacket.OperationName = invokeInfo.OperationSchema.Name; invokePacket.ParameterValues = parameterValues; invokePacket.ParameterTypes = invokeInfo.InvokeParameterSchemas.Select(t => t.ParameterType.FullName).ToArray(); invokePacket.InvokeTime = DateTime.Now; #if SerializePerformance Debug.WriteLine("SerializeInvokePacket Start Time: " + DateTime.Now.ToString("ss:fff")); var invokeTransportableObject = this.Serializer.SerializeInvokePacket(invokePacket); Debug.WriteLine("SerializeInvokePacket End Time: " + DateTime.Now.ToString("ss:fff")); #else var invokeTransportableObject = this.Serializer.SerializeInvokePacket(invokePacket); #endif var returnTransportableObject = this.Serializer.CreateTransportable(); if (this.Transmitter.Transfer(invokeTransportableObject, returnTransportableObject)) { //调用用户自定义过程 #if SerializePerformance Debug.WriteLine("DeSerialize Return Packet Start Time: " + DateTime.Now.ToString("ss:fff")); var returnPacket = this.Serializer.DeSerializeToReturnPacket(returnTransportableObject); result = returnPacket.Content; Debug.WriteLine("DeSerialize Return Packet End Time: " + DateTime.Now.ToString("ss:fff")); #else var returnPacket = this.Serializer.DeSerializeToReturnPacket(returnTransportableObject); result = returnPacket.Content; #endif } } finally { } return(result); }
//interface //处理调用 protected virtual ReturnPacket InvokeHandler(Object instance, InvokePacket packet, InvokeImplementOperationInfo operationInfo) { ReturnPacket returnPacket = new ReturnPacket(); Object returnObject = null; try { returnPacket.HandleTime = DateTime.Now; returnObject = operationInfo.OperationSchema.MethodInfo.Invoke(instance, packet.ParameterValues); returnPacket.Success = true; returnPacket.Content = returnObject; returnPacket.ContentType = operationInfo.ReturnParameterSchema.ParameterType.FullName; } catch (Exception exc) { returnPacket.Success = false; returnPacket.Message = exc.Message; if (this.ExceptionHanppened != null) { HostExceptionEventArgs eventArgs = new HostExceptionEventArgs(); eventArgs.Exception = exc; this.ExceptionHanppened.Invoke(eventArgs); if (!eventArgs.Handled) { throw exc; } } else { throw exc; } } returnPacket.Elapsed = (Int32)(DateTime.Now - returnPacket.HandleTime).TotalMilliseconds; return(returnPacket); }