public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
        {
            JObject json = new JObject();

            json.Add(JsonRpcConstants.MethodKey, this.operation.Name);
            JArray methodParams = new JArray();

            json.Add(JsonRpcConstants.ParamsKey, methodParams);
            for (int i = 0; i < parameters.Length; i++)
            {
                methodParams.Add(null);
            }

            foreach (MessagePartDescription part in this.operation.Messages[0].Body.Parts)
            {
                object paramValue = parameters[part.Index];
                if (paramValue != null)
                {
                    methodParams[part.Index] = JToken.FromObject(paramValue);
                }
            }

            json.Add(JsonRpcConstants.IdKey, new JValue(Interlocked.Increment(ref nextId)));
            return(JsonRpcHelpers.SerializeMessage(json, null));
        }
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            JObject json = JsonRpcHelpers.GetJObjectPreservingMessage(ref reply);

            json[JsonRpcConstants.IdKey] = (int)correlationState;
            reply = JsonRpcHelpers.SerializeMessage(json, reply);
        }
        public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
        {
            JObject json = new JObject();

            json[JsonRpcConstants.ErrorKey]  = null;
            json[JsonRpcConstants.ResultKey] = result == null ? string.Empty : JToken.FromObject(result);
            return(JsonRpcHelpers.SerializeMessage(json, null));
        }
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            JObject json = new JObject();

            json.Add(JsonRpcConstants.ResultKey, null);
            JsonRpcException jsonException = error as JsonRpcException;

            if (jsonException != null)
            {
                json.Add(JsonRpcConstants.ErrorKey, jsonException.JsonException);
            }
            else
            {
                JObject exceptionJson = new JObject
                {
                    { "type", error.GetType().FullName },
                    { "message", error.Message },
                };
                JObject temp = exceptionJson;
                while (error.InnerException != null)
                {
                    error = error.InnerException;
                    JObject innerJson = new JObject
                    {
                        { "type", error.GetType().FullName },
                        { "message", error.Message },
                    };
                    temp["inner"] = innerJson;
                    temp          = innerJson;
                }

                json.Add(JsonRpcConstants.ErrorKey, exceptionJson);
            }

            fault = JsonRpcHelpers.SerializeMessage(json, fault);
        }