ConsumeContext IMessageDeserializer.Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                var messageEncoding = GetMessageEncoding(receiveContext);

                MessageEnvelope envelope;
                using (var body = receiveContext.GetBodyStream())
                    using (var reader = new StreamReader(body, messageEncoding, false, 1024, true))
                        using (var jsonReader = new JsonTextReader(reader))
                        {
                            envelope = _deserializer.Deserialize <MessageEnvelope>(jsonReader);
                        }

                return(new JsonConsumeContext(_deserializer, receiveContext, envelope));
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message envelope", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message envelope", ex);
            }
        }
Beispiel #2
0
        ConsumeContext IMessageDeserializer.Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                MessageEnvelope envelope;
                using (var body = receiveContext.GetBodyStream())
                    using (var jsonReader = new BsonDataReader(body))
                    {
                        envelope = _deserializer.Deserialize <MessageEnvelope>(jsonReader);
                    }

                return(new JsonConsumeContext(_deserializer, receiveContext, envelope));
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message envelope", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message envelope", ex);
            }
        }
Beispiel #3
0
        public ConsumeContext Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                using (var body = receiveContext.GetBodyStream())
                {
                    using (var disposingCryptoStream = _cryptoStreamProvider.GetDecryptStream(body, receiveContext))
                    {
                        using (var jsonReader = new BsonDataReader(disposingCryptoStream))
                        {
                            var messageEnvelope = _deserializer.Deserialize <MessageEnvelope>(jsonReader);

                            return(new JsonConsumeContext(_deserializer, _objectTypeDeserializer, receiveContext,
                                                          messageEnvelope));
                        }
                    }
                }
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException(
                          "A JSON serialization exception occurred while deserializing the message envelope", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message envelope", ex);
            }
        }
Beispiel #4
0
        ConsumeContext IMessageDeserializer.Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                var messageEncoding = GetMessageEncoding(receiveContext);

                using var body       = receiveContext.GetBodyStream();
                using var reader     = new StreamReader(body, messageEncoding, false, 1024, true);
                using var jsonReader = new JsonTextReader(reader);

                var messageToken = _deserializer.Deserialize <JToken>(jsonReader);

                return(new RawConsumeContext(_deserializer, receiveContext, messageToken, _options));
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message", ex);
            }
        }
Beispiel #5
0
        ConsumeContext IMessageDeserializer.Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                var messageEncoding = GetMessageEncoding(receiveContext);

                using var body       = receiveContext.GetBodyStream();
                using var reader     = new StreamReader(body, messageEncoding, false, 1024, true);
                using var jsonReader = new JsonTextReader(reader);

                var messageToken = _deserializer.Deserialize <JToken>(jsonReader);

                Guid?messageId = default;
                if (receiveContext.TransportHeaders.TryGetHeader(/*MessageHeaders.MessageId*/ "MessageId", out var headerValue) &&
                    headerValue is string value && Guid.TryParse(value, out var id))
                {
                    messageId = id;
                }

                return(new RawJsonConsumeContext(_deserializer, receiveContext, messageToken, messageId));
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message", ex);
            }
        }
        public ConsumeContext Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                var messageEncoding = GetMessageEncoding(receiveContext);

                using (var body = receiveContext.GetBodyStream())
                    using (var reader = new StreamReader(body, messageEncoding, false, 1024, true))
                        using (var jsonReader = new JsonTextReader(reader))
                        {
                            var envelope = _deserializer.Deserialize <EventNamespaceMessageEnvelope>(jsonReader);
                            envelope.ApplyEventNamespaceBinding(_messageTypeParser, _eventTypeProvider);
                            return(new JsonConsumeContext(_deserializer, receiveContext, envelope));
                        }
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message envelope", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message envelope", ex);
            }
        }
Beispiel #7
0
 void IMessageSerializer.Serialize <T>(Stream stream, SendContext <T> context)
 {
     using (var bodyStream = _context.GetBodyStream())
     {
         bodyStream.CopyTo(stream);
     }
 }
Beispiel #8
0
        ConsumeContext IMessageDeserializer.Deserialize(ReceiveContext receiveContext)
        {
            object obj;
            var headers = new Header[0];
            using (Stream body = receiveContext.GetBodyStream())
            {
                obj = _formatter.Deserialize(body, x => headers = x);
            }

            return new StaticConsumeContext(receiveContext, obj, headers);
        }
        ConsumeContext IMessageDeserializer.Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                XDocument document;
                long      position;
                using (var body = receiveContext.GetBodyStream())
                {
                    using (var xmlReader = XmlReader.Create(body, new XmlReaderSettings {
                        CheckCharacters = false
                    }))
                    {
                        document = XDocument.Load(xmlReader);

                        position = body.Position;
                    }
                }

                var json = new StringBuilder((int)position);

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

                        XmlMessageSerializer.XmlSerializer.Serialize(jsonWriter, document.Root);
                    }

                using (var stringReader = new StringReader(json.ToString()))
                    using (var jsonReader = new JsonTextReader(stringReader))
                    {
                        var messageToken = _deserializer.Deserialize <JToken>(jsonReader);

                        return(new NServiceBusConsumeContext(_deserializer, receiveContext, messageToken));
                    }
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message", ex);
            }
        }
        protected Task Move(ReceiveContext context, Action <BrokeredMessage, SendHeaders> preSend)
        {
            IPipe <SendEndpointContext> clientPipe = Pipe.ExecuteAsync <SendEndpointContext>(async clientContext =>
            {
                if (!context.TryGetPayload(out BrokeredMessageContext messageContext))
                {
                    throw new ArgumentException("The ReceiveContext must contain a BrokeredMessageContext (from Azure Service Bus)", nameof(context));
                }

                using (var messageBodyStream = context.GetBodyStream())
                    using (var message = new BrokeredMessage(messageBodyStream)
                    {
                        ContentType = context.ContentType.MediaType,
                        ForcePersistence = messageContext.ForcePersistence,
                        TimeToLive = messageContext.TimeToLive,
                        CorrelationId = messageContext.CorrelationId,
                        MessageId = messageContext.MessageId,
                        Label = messageContext.Label,
                        PartitionKey = messageContext.PartitionKey,
                        ReplyTo = messageContext.ReplyTo,
                        ReplyToSessionId = messageContext.ReplyToSessionId,
                        SessionId = messageContext.SessionId
                    })
                    {
                        SendHeaders headers = new DictionarySendHeaders(message.Properties);

                        foreach (KeyValuePair <string, object> property in messageContext.Properties)
                        {
                            headers.Set(property.Key, property.Value);
                        }

                        headers.SetHostHeaders();

                        preSend(message, headers);

                        await clientContext.Send(message).ConfigureAwait(false);

                        var reason = message.Properties.ContainsKey(MessageHeaders.Reason) ? message.Properties[MessageHeaders.Reason].ToString() : "";
                        if (reason == "fault")
                        {
                            reason = message.Properties.ContainsKey(MessageHeaders.FaultMessage) ? $"Fault: {message.Properties[MessageHeaders.FaultMessage]}" : "Fault";
                        }

                        context.LogMoved(clientContext.EntityPath, reason);
                    }
            });

            return(_source.Send(clientPipe, context.CancellationToken));
        }
        protected Task Move(ReceiveContext context, Action <Message, IDictionary <string, object> > preSend)
        {
            IPipe <SendEndpointContext> clientPipe = Pipe.ExecuteAsync <SendEndpointContext>(async clientContext =>
            {
                if (!context.TryGetPayload(out BrokeredMessageContext messageContext))
                {
                    throw new ArgumentException("The ReceiveContext must contain a BrokeredMessageContext (from Azure Service Bus)", nameof(context));
                }

                using (var messageBodyStream = context.GetBodyStream())
                {
                    var message = new Message(messageBodyStream.ReadAsBytes())
                    {
                        ContentType      = context.ContentType.MediaType,
                        TimeToLive       = messageContext.TimeToLive,
                        CorrelationId    = messageContext.CorrelationId,
                        MessageId        = messageContext.MessageId,
                        Label            = messageContext.Label,
                        PartitionKey     = messageContext.PartitionKey,
                        ReplyTo          = messageContext.ReplyTo,
                        ReplyToSessionId = messageContext.ReplyToSessionId,
                        SessionId        = messageContext.SessionId
                    };

                    foreach (KeyValuePair <string, object> property in messageContext.Properties.Where(x => !x.Key.StartsWith("MT-")))
                    {
                        message.UserProperties.Set(new HeaderValue(property.Key, property.Value));
                    }

                    message.UserProperties.SetHostHeaders();

                    preSend(message, message.UserProperties);

                    await clientContext.Send(message).ConfigureAwait(false);

                    var reason = message.UserProperties.TryGetValue(MessageHeaders.Reason, out var reasonProperty) ? reasonProperty.ToString() : "";
                    if (reason == "fault")
                    {
                        reason = message.UserProperties.TryGetValue(MessageHeaders.FaultMessage, out var fault) ? $"Fault: {fault}" : "Fault";
                    }

                    context.LogMoved(clientContext.EntityPath, reason);
                }
            });

            return(_supervisor.Send(clientPipe, context.CancellationToken));
        }
        ConsumeContext IMessageDeserializer.Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                XDocument document;
                using (var body = receiveContext.GetBodyStream())
                    using (var xmlReader = XmlReader.Create(body, new XmlReaderSettings {
                        CheckCharacters = false
                    }))
                    {
                        document = XDocument.Load(xmlReader);
                    }

                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;

                        XmlMessageSerializer.XmlSerializer.Serialize(jsonWriter, document.Root);
                    }

                MessageEnvelope envelope;
                using (var stringReader = new StringReader(json.ToString()))
                    using (var jsonReader = new JsonTextReader(stringReader))
                    {
                        envelope = _deserializer.Deserialize <MessageEnvelope>(jsonReader);
                    }

                return(new JsonConsumeContext(_deserializer, receiveContext, envelope));
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message envelope", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message envelope", ex);
            }
        }
        public ConsumeContext Deserialize(ReceiveContext receiveContext)
        {
            var deserializer    = JsonMessageSerializer.Deserializer;
            var messageEncoding = GetMessageEncoding(receiveContext);

            using (var body = receiveContext.GetBodyStream())
            {
                using (var reader = new StreamReader(body, messageEncoding, false, 1024, true))
                {
                    using (var jsonReader = new JsonTextReader(reader))
                    {
                        var msg      = deserializer.Deserialize <JToken>(jsonReader);
                        var envelope = new MsgEnvelope
                        {
                            Message     = msg,
                            MessageType = new string[0],
                        };
                        return(new GenericJsonConsumeContext(JsonMessageSerializer.Deserializer, receiveContext, envelope));
                    }
                }
            }
        }
        public ConsumeContext Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                var messageEncoding = GetMessageEncoding(receiveContext);
                CloudEventMessageEnvelope cloudEventMessageEnvelope;

                using var body = receiveContext.GetBodyStream();

                using var reader = new StreamReader(body, messageEncoding, false, 1024, true);
                using (var jsonReader = new JsonTextReader(reader))
                {
                    cloudEventMessageEnvelope = JsonMessageSerializer.Deserializer.Deserialize <CloudEventMessageEnvelope>(jsonReader);
                }

                if (!cloudEventMessageEnvelope.Type.Equals(MessageSourceType))
                {
                    throw new SerializationException($"Message source should originate from Dapr ({MessageSourceType})");
                }

                // Upwrap the CloudEvent envelope and continue as normal with the MassTransit envelope.
                var massTransitEnvelope = cloudEventMessageEnvelope.Data;

                return(new JsonConsumeContext(JsonMessageSerializer.Deserializer, receiveContext, massTransitEnvelope));
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message", ex);
            }
        }
Beispiel #15
0
        ConsumeContext IMessageDeserializer.Deserialize(ReceiveContext receiveContext)
        {
            try
            {
                using var body       = receiveContext.GetBodyStream();
                using var jsonReader = new BsonDataReader(body);

                var messageToken = _deserializer.Deserialize <JToken>(jsonReader);

                return(new GrpcConsumeContext(_deserializer, receiveContext, messageToken));
            }
            catch (JsonSerializationException ex)
            {
                throw new SerializationException("A JSON serialization exception occurred while deserializing the message", ex);
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("An exception occurred while deserializing the message", ex);
            }
        }