public void Serialize <T>(Stream output, ISendContext <T> context)
            where T : class
        {
            try
            {
                context.SetContentType(ContentTypeHeaderValue);

                Envelope envelope = Envelope.Create(context);

                using (var nonClosingStream = new NonClosingStream(output))
                    using (var writer = new StreamWriter(nonClosingStream))
                        using (var jsonWriter = new JsonTextWriter(writer))
                        {
                            jsonWriter.Formatting = Formatting.Indented;

                            Serializer.Serialize(jsonWriter, envelope);

                            jsonWriter.Flush();
                            writer.Flush();
                        }
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("Failed to serialize message", ex);
            }
        }
Beispiel #2
0
        public void Serialize <T>(Stream stream, ISendContext <T> context)
            where T : class
        {
            try
            {
                context.SetContentType(ContentTypeHeaderValue);

                XmlMessageEnvelope envelope = XmlMessageEnvelope.Create(context);

                _serializer.Serialize(stream, envelope, (declaringType, propertyType, value) =>
                {
                    if (declaringType == typeof(XmlMessageEnvelope) && propertyType == typeof(object))
                    {
                        return(typeof(T));
                    }

                    if (propertyType == typeof(object))
                    {
                        return(value.GetType());
                    }

                    return(propertyType);
                });
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("Failed to serialize message", ex);
            }
        }
Beispiel #3
0
        public void Serialize <T>(Stream output, ISendContext <T> context)
            where T : class
        {
            CheckConvention.EnsureSerializable(context.Message);

            _formatter.Serialize(output, context.Message, GetHeaders(context));

            context.SetContentType(ContentTypeHeaderValue);
        }
Beispiel #4
0
        public void Serialize <T>(Stream output, ISendContext <T> context) where T : class
        {
            context.SetContentType(ContentTypeHeaderValue);
            Envelope envelope = Envelope.Create(context);

            using (var outputStream = new NonClosingStream(output))
            {
                ProtoBuf.Serializer.Serialize(outputStream, envelope);
            }
        }
        public void Serialize <T>(Stream output, ISendContext <T> context)
            where T : class
        {
            context.SetContentType(ContentTypeHeaderValue);

            Envelope envelope = Envelope.Create(context);

            using (var outputStream = new NonClosingStream(output))
                using (var bsonWriter = new BsonWriter(outputStream))
                {
                    Serializer.Serialize(bsonWriter, envelope);

                    bsonWriter.Flush();
                }
        }
Beispiel #6
0
        public void Serialize <T>(Stream output, ISendContext <T> context)
            where T : class
        {
            try
            {
                context.SetContentType(ContentTypeHeaderValue);

                Envelope envelope = Envelope.Create(context);

                var json = new StringBuilder(1024);

                using (var stringWriter = new StringWriter(json, CultureInfo.InvariantCulture))
                    using (var jsonWriter = new JsonTextWriter(stringWriter))
                    {
                        jsonWriter.Formatting = Newtonsoft.Json.Formatting.None;

                        JsonMessageSerializer.Serializer.Serialize(jsonWriter, envelope);

                        jsonWriter.Flush();
                        stringWriter.Flush();
                    }

                using (var stringReader = new StringReader(json.ToString()))
                    using (var jsonReader = new JsonTextReader(stringReader))
                    {
                        var document = (XDocument)XmlSerializer.Deserialize(jsonReader, typeof(XDocument));

                        using (var nonClosingStream = new NonClosingStream(output))
                            using (var streamWriter = new StreamWriter(nonClosingStream))
                                using (var xmlWriter = new XmlTextWriter(streamWriter))
                                {
                                    document.WriteTo(xmlWriter);
                                }
                    }
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("Failed to serialize message", ex);
            }
        }