Esempio n. 1
0
        private static Message Serialize(
            WebFormatterFactory formatterFactory,
            object result,
            Type responseType,
            string[] contentTypes)
        {
            string resolvedContentType;

            var formatter            = formatterFactory.CreateFormatter(contentTypes, out resolvedContentType);
            var serializationContext = formatter.Serialize(result, responseType);

            Message message = null;

            switch (serializationContext.ContentFormat)
            {
            case WebFormatterSerializationContext.SerializationFormat.Xml:
                message = Message.CreateMessage(MessageVersion.None, null, result, serializationContext.XmlSerializer);
                message.SetWebContentFormatProperty(WebContentFormat.Xml);
                break;

            case WebFormatterSerializationContext.SerializationFormat.Json:
                message = Message.CreateMessage(MessageVersion.None, null, result, serializationContext.XmlSerializer);
                message.SetWebContentFormatProperty(WebContentFormat.Json);
                break;

            case WebFormatterSerializationContext.SerializationFormat.Binary:
                message = Message.CreateMessage(MessageVersion.None, null, new BinaryBodyWriter(serializationContext.BinaryData));
                message.SetWebContentFormatProperty(WebContentFormat.Raw);
                break;
            }

            WebOperationContext.Current.OutgoingResponse.ContentType = resolvedContentType;

            return(message);
        }
Esempio n. 2
0
        public WebDispatchFormatter(
            WebFormatterFactory formatterFactory,
            OperationDescription operationDescription,
            IDispatchMessageFormatter originalFormatter,
            FormatterDirection direction)
        {
            _formatterFactory  = formatterFactory;
            _requestParameters = operationDescription.GetRequestMessageParts();
            _responseType      = operationDescription.GetResponseType();
            _direction         = direction;

            if (direction != FormatterDirection.Both)
            {
                _originalFormatter = originalFormatter;
            }
        }
Esempio n. 3
0
        private static void Deserialize(
            Message message,
            WebFormatterFactory formatterFactory,
            ref object[] parameters,
            RequestMessagePartDescription[] requestParameters,
            string contentType)
        {
            // Grab the actual uri values, this will be null if there are none
            var uriParameters = message.GetRequestUriTemplateMatchVariables();

            // Loop through the parameters and set them based on their type
            for (var i = 0; i < parameters.Length; i++)
            {
                switch (requestParameters[i].PartType)
                {
                // If its a path segment it will always be a string so just set it
                case RequestMessagePartDescription.MessagePartType.PathSegment:
                    if (uriParameters != null)
                    {
                        parameters[i] = uriParameters[requestParameters[i].Name];
                    }
                    break;

                // If its a querystring value it can be any primitive type so we need to
                // perform a conversion.
                case RequestMessagePartDescription.MessagePartType.Querystring:
                    if (uriParameters != null)
                    {
                        try
                        {
                            parameters[i] = QueryStringConverter.ConvertStringToValue(
                                uriParameters[requestParameters[i].Name],
                                requestParameters[i].Type);
                        }
                        catch (Exception exception)
                        {
                            throw new DeserializationException(exception,
                                                               "The querystring parameter '{0}' must be of type '{1}'.",
                                                               requestParameters[i].Alias,
                                                               requestParameters[i].Type.GetXsdType());
                        }
                    }
                    break;

                // If it's the entity body then we need to deserialize it
                case RequestMessagePartDescription.MessagePartType.EntityBody:
                    if (message.IsEmpty)
                    {
                        continue;
                    }
                    var formatter = formatterFactory.CreateFormatter(contentType);
                    using (var reader = message.GetReaderAtBodyContents())
                    {
                        var deserializationContext = reader.Name == BinaryBodyReader.BinaryElementName ?
                                                     WebFormatterDeserializationContext.CreateBinary(new BinaryBodyReader(reader).Data) :
                                                     WebFormatterDeserializationContext.CreateXml(reader);

                        try
                        {
                            parameters[i] = formatter.Deserialize(deserializationContext, requestParameters[i].Type);
                        }
                        catch (Exception exception)
                        {
                            throw new DeserializationException(exception);
                        }
                    }
                    break;
                }
            }
        }