public void Intercept(IInvocation invocation) { // Create JSON representation for MethodInfo object JMethodInfo info = new JMethodInfo(invocation.Method, invocation.Arguments); // Serialize the JSON to byte array byte[] data = Serializer.SerializeObject(info); // Consume service using the injected consumer byte[] response = m_Consumer.ConsumeService(data); // Get the JSON representation for the server response JResponse jResponse = Serializer.DeserializeObject <JResponse>(response); switch (jResponse.ResponseType) { case ResponseType.Empty: return; case ResponseType.Success: // If expected ReturnType is not void, assign the return value if (invocation.Method.ReturnType != typeof(void)) { // Make sure return type is equal to response value type if (invocation.Method.ReturnType != jResponse.ReturnValue.Value.GetType()) { // if not, deserialize the value to this type explicitly jResponse.ReturnValue.Value = Serializer.DeserializeObject(jResponse.ReturnValue.Value, invocation.Method.ReturnType); } // Set the return value invocation.ReturnValue = jResponse.ReturnValue.Value; } break; case ResponseType.Fault: JException jException = Serializer.DeserializeObject <JException>(jResponse.ReturnValue.Value); if (jException != null) { throw jException; } throw new TargetInvocationException(null); default: throw new ArgumentOutOfRangeException(); } }
private byte[] InnerOnServiceRequest(byte[] buffer) { // Translate data bytes to a JMethodInfo object. JMethodInfo jMethodInfo = Serializer.DeserializeObject <JMethodInfo>(buffer); // Get register service object instance = m_RegisteredServices.FirstOrDefault(x => x.Key.FullName == jMethodInfo.DeclaringType.FullName).Value; if (instance == null) { throw new InvalidOperationException($"Cannot find requested service {jMethodInfo.DeclaringType.FullName}"); } // Get method parameters and parameters value Type[] parameterTypes = jMethodInfo.Parameters.Select(p => Type.GetType(p.ParameterType.AssemblyQualifiedName)).ToArray(); object[] parameters = jMethodInfo.Parameters.Select(p => p.Value).ToArray(); // Make sure all parameters represented in the expected type for (int i = 0; i < parameters.Length; ++i) { if (parameters[i].GetType() != parameterTypes[i]) { parameters[i] = Serializer.DeserializeObject(parameters[i], parameterTypes[i]); } } // Get service method to invoke MethodInfo methodToInvoke = instance.GetType().GetMethod(jMethodInfo.Name, parameterTypes); if (methodToInvoke == null) { throw new InvalidOperationException($"Cannot find requested method {jMethodInfo.Name}"); } // Invoke service method object returnValue = methodToInvoke.Invoke(instance, parameters); // return method response as byte array or empty array in the return type is void return(returnValue == null?Serializer.SerializeObject(new JResponse()) : Serializer.SerializeObject(new JResponse(returnValue))); }