Ejemplo n.º 1
0
        public static Message AsWcfMessage(this RabbitMqMessage currentRabbitMqMessage, long parentMaxReceivedMessageSize, BufferManager parentBufferManager, MessageEncoderFactory messageEncoderFactory)
        {
            Debug.Assert(currentRabbitMqMessage != null);
            byte[] data;
            long   bytesTotal;

            using (Stream stream = new MemoryStream())
            {
                stream.Write(currentRabbitMqMessage.Body, 0, currentRabbitMqMessage.Body.Length);

                stream.Position = 0;

                bytesTotal = stream.Length;

                if (bytesTotal > int.MaxValue)
                {
                    throw new CommunicationException(
                              $"Message of size {bytesTotal} bytes is too large to buffer. Use a streamed transfer instead.");
                }

                if (bytesTotal > parentMaxReceivedMessageSize)
                {
                    throw new CommunicationException(
                              $"Message exceeds maximum size: {bytesTotal} > {parentMaxReceivedMessageSize}.");
                }

                data = parentBufferManager.TakeBuffer((int)bytesTotal);

                var bytesRead = 0;

                while (bytesRead < bytesTotal)
                {
                    var count = stream.Read(data, bytesRead, (int)bytesTotal - bytesRead);

                    if (count == 0)
                    {
                        throw new CommunicationException($"Unexpected end of message after {bytesRead} of {bytesTotal} bytes.");
                    }

                    bytesRead += count;
                }
            }

            var buffer = new ArraySegment <byte>(data, 0, (int)bytesTotal);

            var message = messageEncoderFactory.Encoder.ReadMessage(buffer, parentBufferManager);

            parentBufferManager.ReturnBuffer(data);

            return(message);
        }
Ejemplo n.º 2
0
        internal static RabbitMqMessage AsRabbitMqMessage(this Message message)
        {
            RabbitMqMessage rabbitMsg;

            using (Stream stream = new MemoryStream())
            {
                var writer = XmlWriter.Create(stream);

                message.WriteMessage(writer);

                writer.Flush();

                stream.Position = 0;

                var bytes = new byte[stream.Length];

                stream.Read(bytes, 0, (int)stream.Length);

                var actionComponents = message.Headers.Action.Split('/');
                var serviceName      = actionComponents[actionComponents.Length - 2];
                var correlationId    = Guid.NewGuid().ToString();
                var replyTopic       = $"{serviceName}{ServiceConfiguration.ReplyExchangeSuffix}";

                IBasicProperties props = new BasicProperties
                {
                    MessageId     = message.Headers.MessageId?.ToString() ?? Guid.NewGuid().ToString(),
                    CorrelationId = correlationId,
                    ReplyTo       = replyTopic,
                    Headers       = new Dictionary <string, object>()
                };

                rabbitMsg = new RabbitMqMessage
                {
                    Body            = bytes,
                    Topic           = $"{serviceName}{ServiceConfiguration.DirectExchangeSuffix}",
                    ReplyTopic      = replyTopic,
                    ReplyRoutingKey = "",
                    RoutingKey      = ServiceConfiguration.ServiceProcessingRoute,
                    Properties      = props,
                    CorrelationId   = correlationId
                };
            }
            return(rabbitMsg);
        }