コード例 #1
0
        private void SendMessage <T>(T message, IMessageQueueTransaction messageQueueTransaction)
        {
            var jsonSerializerSettings = new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.All
            };

            var queueMessage = new QueueMessage {
                MessageBody = message.SerializeToJson(jsonSerializerSettings), MessageType = typeof(T)
            };

            using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(queueMessage.SerializeToJson(jsonSerializerSettings))))
            {
                var buffer         = new byte[MaxMsmqMessageSize];
                var correlationId  = "";
                var messageSize    = memoryStream.Length;
                var numberOfChunks = Convert.ToInt32(Math.Ceiling((double)messageSize / buffer.Length));
                int bytes;
                var chunk = 0;

                Logger.Debug("Message send {QueueName} {Type} {%Message}", Name, queueMessage.MessageType.FullName, queueMessage.MessageBody);

                if (!Transactional && numberOfChunks > 1)
                {
                    throw new MessageSizeException(memoryStream.Length, buffer.Length, Transactional);
                }

                while ((bytes = memoryStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    var mqMessage = new Message
                    {
                        AppSpecific   = numberOfChunks,
                        CorrelationId = correlationId
                    };

                    mqMessage.BodyStream.Write(buffer, 0, bytes);

                    Logger.Debug("Sending message chunk ({Chunk}/{NumberOfChunks}) on {QueueName} {CorrelationId}", ++chunk, numberOfChunks, Name, correlationId);

                    MessageQueueManager.Send(MessageQueue, mqMessage, messageQueueTransaction?.Transaction);

                    correlationId = mqMessage.Id;
                }
            }
        }
コード例 #2
0
        private static Message GetMessage(object dto)
        {
            var body = new QueueMessage {
                MessageBody = dto.SerializeToJson(new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All
                }), MessageType = dto.GetType()
            };

            var message = new Message
            {
                Body        = body,
                AppSpecific = 1,
                BodyStream  = new MemoryStream(Encoding.UTF8.GetBytes(body.SerializeToJson(new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All
                })))
            };

            return(message);
        }
コード例 #3
0
        private void AddMessageToQueue(System.Messaging.MessageQueue messageQueue, object dto)
        {
            var body = new QueueMessage {
                MessageBody = dto.SerializeToJson(new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All
                }), MessageType = dto.GetType()
            };

            var message = new Message
            {
                Body        = body,
                AppSpecific = 1,
                BodyStream  = new MemoryStream(Encoding.UTF8.GetBytes(body.SerializeToJson(new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All
                })))
            };

            _messageQueueManager.Receive(messageQueue, Arg.Any <TimeSpan>(), Arg.Any <System.Messaging.MessageQueueTransaction>()).Returns(message);
            _messageQueueManager.EndPeek(messageQueue, Arg.Any <IAsyncResult>()).Returns(message);
            _messageQueueManager.BeginPeek(messageQueue, Arg.Any <TimeSpan>()).Returns((IAsyncResult)null);
        }