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);
        }
Exemple #2
0
        public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
        {
            JsonRpcResponse <object> jsonResponse = CreateResponse(result);
            Encoding encoding = GetResponseMessageEncoding();

            byte[] rawBody = DispatcherUtils.SerializeBody(jsonResponse, encoding);

            return(DispatcherUtils.CreateMessage(messageVersion, _responseMessage.Action, rawBody, encoding));
        }
        public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
        {
            JsonRpcRequest jsonRequest = CreateRequest(parameters);

            byte[] rawBody = DispatcherUtils.SerializeBody(jsonRequest, Encoding.UTF8);

            Message requestMessage = DispatcherUtils.CreateMessage(
                messageVersion, _requestMessage.Action, rawBody, Encoding.UTF8);

            requestMessage.Headers.To = _endpoint.Address.Uri;
            requestMessage.Properties.Add(DispatcherUtils.OperationNameKey, _operation.Name);

            return(requestMessage);
        }
Exemple #4
0
 public void BeforeSendReply(ref Message reply, object correlationState)
 {
     if (reply.IsFault)
     {
         MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
         var           fault  = MessageFault.CreateFault(buffer.CreateMessage(), Int32.MaxValue);
         if (fault.Code.SubCode.Name == "ActionNotSupported")
         {
             // TODO message id
             reply = DispatcherUtils.CreateErrorMessage(reply.Version, null,
                                                        (int)JsonRpcErrorCodes.MethodNotFound, "Method not found.",
                                                        fault.Reason.GetMatchingTranslation().Text);
         }
         // TODO process other fault codes
     }
 }
Exemple #5
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;
        }
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            bool includeDetails = IncludeExceptionDetails();

            object msgId = null;

            if (OperationContext.Current.IncomingMessageProperties.ContainsKey(DispatcherUtils.MessageIdKey))
            {
                msgId = OperationContext.Current.IncomingMessageProperties[DispatcherUtils.MessageIdKey];
            }

            var jsonRpcError = error as JsonRpcException;

            if (jsonRpcError != null)
            {
                fault = DispatcherUtils.CreateErrorMessage(version, msgId, jsonRpcError);
            }
            else
            {
                // TODO: extract exception details from FaultException
                object additionalData;
                var    faultException = error as FaultException;
                if (faultException != null && faultException.GetType().IsGenericType)
                {
                    additionalData = faultException.GetType().GetProperty("Detail").GetValue(faultException, null);
                }
                else
                {
                    additionalData = error;
                }

                // TODO: check error type and set appropriate error code
                fault = DispatcherUtils.CreateErrorMessage(version, msgId,
                                                           (int)JsonRpcErrorCodes.ServerError, error.Message, additionalData);
            }
        }