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 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 object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            JObject json      = JsonRpcHelpers.GetJObjectPreservingMessage(ref request);
            int     requestId = json[JsonRpcConstants.IdKey].Value <int>();

            return(requestId);
        }
        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 object DeserializeReply(Message message, object[] parameters)
        {
            JObject json = JsonRpcHelpers.DeserializeMessage(message);

            return(JsonConvert.DeserializeObject(
                       json[JsonRpcConstants.ResultKey].ToString(),
                       this.operation.Messages[1].Body.ReturnValue.Type));
        }
 public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
 {
     clientRuntime.MessageInspectors.Add(new JsonRpcMessageInspector());
     foreach (OperationDescription operation in endpoint.Contract.Operations)
     {
         if (!JsonRpcHelpers.IsUntypedMessage(operation))
         {
             ClientOperation clientOperation = clientRuntime.Operations[operation.Name];
             clientOperation.SerializeRequest = true;
             clientOperation.DeserializeReply = true;
             clientOperation.Formatter        = new JsonRpcMessageFormatter(operation);
         }
     }
 }
        public void DeserializeRequest(Message message, object[] parameters)
        {
            JObject json       = JsonRpcHelpers.DeserializeMessage(message);
            JArray  jsonParams = json[JsonRpcConstants.ParamsKey] as JArray;

            foreach (MessagePartDescription part in this.operation.Messages[0].Body.Parts)
            {
                int index = part.Index;
                if (jsonParams[index].Type != JTokenType.Null)
                {
                    parameters[index] = JsonConvert.DeserializeObject(
                        jsonParams[index].ToString(),
                        part.Type);
                }
            }
        }
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            JObject json      = JsonRpcHelpers.GetJObjectPreservingMessage(ref reply);
            int     replyId   = json[JsonRpcConstants.IdKey].Value <int>();
            int     requestId = (int)correlationState;

            if (replyId != requestId)
            {
                throw new JsonRpcException("id mismatch", "Reply does not correspond to the request!");
            }

            if (json[JsonRpcConstants.ErrorKey].Type != JTokenType.Null)
            {
                throw new JsonRpcException(json[JsonRpcConstants.ErrorKey]);
            }
        }
 public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
 {
     endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new JsonRpcMessageInspector());
     endpointDispatcher.DispatchRuntime.OperationSelector = new JsonRpcOperationSelector();
     endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new JsonRpcErrorHandler());
     endpointDispatcher.ContractFilter = new MatchAllMessageFilter();
     foreach (OperationDescription operation in endpoint.Contract.Operations)
     {
         if (!JsonRpcHelpers.IsUntypedMessage(operation))
         {
             DispatchOperation dispatchOperation = endpointDispatcher.DispatchRuntime.Operations[operation.Name];
             dispatchOperation.DeserializeRequest = true;
             dispatchOperation.SerializeReply     = true;
             dispatchOperation.Formatter          = new JsonRpcMessageFormatter(operation);
         }
     }
 }
        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);
        }
        public string SelectOperation(ref Message message)
        {
            JObject json = JsonRpcHelpers.GetJObjectPreservingMessage(ref message);

            return(json[JsonRpcConstants.MethodKey].Value <string>());
        }
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            JObject json = JsonRpcHelpers.GetJObjectPreservingMessage(ref request);

            return(json[JsonRpcConstants.IdKey].Value <int>());
        }