Inheritance: MessageEnvelope
コード例 #1
0
        public void Serialize <T>(Stream stream, SendContext <T> context)
            where T : class
        {
            try
            {
                context.ContentType = JsonContentType;

                var envelope = new JsonMessageEnvelope(context, context.Message, TypeMetadataCache <T> .MessageTypeNames);

                using (var writer = new StreamWriter(stream, _encoding.Value, 1024, true))
                    using (var jsonWriter = new JsonTextWriter(writer))
                    {
                        jsonWriter.Formatting = Formatting.Indented;

                        _serializer.Value.Serialize(jsonWriter, envelope, typeof(MessageEnvelope));

                        jsonWriter.Flush();
                        writer.Flush();
                    }
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("Failed to serialize message", ex);
            }
        }
コード例 #2
0
        void IMessageSerializer.Serialize <T>(Stream stream, SendContext <T> context)
        {
            context.ContentType = EncryptedContentType;

            var envelope = new JsonMessageEnvelope(context, context.Message, TypeMetadataCache <T> .MessageTypeNames);

            using Stream cryptoStream = _streamProvider.GetEncryptStream(stream, context);
            using var jsonWriter      = new BsonDataWriter(cryptoStream);

            _serializer.Serialize(jsonWriter, envelope, typeof(MessageEnvelope));

            jsonWriter.Flush();
        }
コード例 #3
0
        public void Serialize <T>(Stream stream, SendContext <T> context)
            where T : class
        {
            context.ContentType = BsonContentType;

            var envelope = new JsonMessageEnvelope(context, context.Message, TypeMetadataCache <T> .MessageTypeNames);

            using (var jsonWriter = new BsonDataWriter(stream))
            {
                _serializer.Value.Serialize(jsonWriter, envelope, typeof(MessageEnvelope));

                jsonWriter.Flush();
            }
        }
コード例 #4
0
        void IMessageSerializer.Serialize <T>(Stream stream, SendContext <T> context)
        {
            context.ContentType = EncryptedContentType;

            var envelope = new JsonMessageEnvelope(context, context.Message, TypeMetadataCache <T> .MessageTypeNames);

            using var cryptoStream = _streamProvider.GetEncryptStream(stream, context);
            using (var jsonWriter = new BsonDataWriter(cryptoStream))
            {
                _serializer.Serialize(jsonWriter, envelope, typeof(MessageEnvelope));

                jsonWriter.Flush();

                ((System.Security.Cryptography.CryptoStream)cryptoStream).FlushFinalBlock();
            }
        }
コード例 #5
0
        public void Serialize <T>(Stream stream, SendContext <T> context)
            where T : class
        {
            try
            {
                context.ContentType = XmlContentType;

                var envelope = new JsonMessageEnvelope(context, context.Message, TypeMetadataCache <T> .MessageTypeNames);

                var json = new StringBuilder(1024);

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

                        JsonMessageSerializer.Serializer.Serialize(jsonWriter, envelope, typeof(MessageEnvelope));

                        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 writer = new StreamWriter(stream, _encoding.Value, 1024, true))
                            using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings {
                                CheckCharacters = false
                            }))
                            {
                                document.WriteTo(xmlWriter);
                            }
                    }
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("Failed to serialize message", ex);
            }
        }
コード例 #6
0
        public void Serialize <T>(Stream stream, SendContext <T> context)
            where T : class
        {
            try
            {
                context.ContentType = XmlContentType;

                var envelope = new JsonMessageEnvelope(context, context.Message, TypeMetadataCache <T> .MessageTypeNames);

                Serialize(stream, envelope, typeof(MessageEnvelope));
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("Failed to serialize message", ex);
            }
        }
コード例 #7
0
        public void Serialize <T>(Stream stream, SendContext <T> context)
            where T : class
        {
            try
            {
                context.ContentType = JsonContentType;

                var envelope = new JsonMessageEnvelope(context, context.Message, TypeMetadataCache <T> .MessageTypeNames);

                using var writer = new Utf8JsonWriter(stream);

                JsonSerializer.Serialize(writer, envelope, Options);

                writer.Flush();
            }
            catch (Exception ex)
            {
                throw new SerializationException("Failed to serialize message", ex);
            }
        }