public object DeserializeReply(Message message, object[] parameters)
        {
            var  property = (HttpResponseMessageProperty)message.Properties[HttpResponseMessageProperty.Name];
            bool isError  = ((int)property.StatusCode >= 400);

            IJsonRpcResponseResult body;
            Type destType = _responseMessage.Body.ReturnValue.Type;

            if (typeof(void) == destType && !isError)
            {
                return(null);
            }

            byte[] rawBody      = DispatcherUtils.DeserializeBody(message);
            var    responseType = typeof(JsonRpcResponse <>).MakeGenericType(destType);

            using (var bodyReader = new StreamReader(new MemoryStream(rawBody))) {
                var serializer = new JsonSerializer();
                body = (IJsonRpcResponseResult)serializer.Deserialize(bodyReader, responseType);
            }

            // TODO: check id

            if (isError)
            {
                if (body.Error != null)
                {
                    throw body.Error;
                }
                // TODO
                throw new Exception();
            }

            return(body.Result);
        }
Example #2
0
        public void DeserializeRequest(Message message, object[] parameters)
        {
            // TODO: check message format (raw)

            byte[] rawBody = DispatcherUtils.DeserializeBody(message);

            JsonRpcRequest body;

            using (var bodyReader = new StreamReader(new MemoryStream(rawBody))) {
                var serializer = new JsonSerializer();
                body = (JsonRpcRequest)serializer.Deserialize(bodyReader, typeof(JsonRpcRequest));
            }

            var paramValues = body.Params as JObject;

            if ((paramValues == null && parameters.Length > 0) ||
                (paramValues != null && paramValues.Count < parameters.Length))
            {
                throw new JsonRpcException((int)JsonRpcErrorCodes.InvalidRequest,
                                           "Insufficient parameters count.", null);
            }

            int paramIndex = 0;

            foreach (var parameter in _requestMessage.Body.Parts)
            {
                JToken value;
                if (paramValues.TryGetValue(parameter.Name, out value))
                {
                    try {
                        parameters[paramIndex] = value.ToObject(parameter.Type);
                    } catch (Exception ex) {
                        throw new JsonRpcException((int)JsonRpcErrorCodes.InvalidParams, ex.Message, ex);
                    }
                }

                ++paramIndex;
            }

            message.Properties[DispatcherUtils.MessageIdKey] = body.Id;
        }