2

При использовании MSMQ я не могу передать сообщение больше чем 4 мб (ограничение). Как мне обойти это ограничение ?

2 ответа 2

3

Судя по всему, мы хотите использовать шину для проброски файлов. На мой взгляд использовать для этого шину не лучшая идея, однако если хочется, вот пример от Ms:

using System;
using System.Messaging;
using System.IO;
namespace FilesInMsmq{
    /// <summary> 
    /// This example shows how to send and receive Files larger than 4 Mb. using MSMQ Messages.
    /// How to run the example:
    /// FilesInMsmq [send/receive] [File Name] [Queue Path] 
    /// </summary> 
    class MsmqFileExample {
        //break the mesage into 4 Mb chunks  static int chunkSize = 4194000;
        [STAThread]  static void Main(string[] args)  {
            string fileName;
            string queuePath;
            if (args.Length > 0)   {
                //get parameters from command prompt  
                fileName = args[1];
                queuePath = args[2];
                switch (args[0])    {
                case "send":
                    SendFile(fileName, queuePath);
                    break;
                case "receive":
                        ReceiveFile(fileName, queuePath);
                        break;
                }
            }
        }
        static void SendFile(string fileName, string queuePath)  {
            int i;
            int count = 0;
            int msgNumber = 0;
            //Open an existing queue 
            MessageQueue queue = new MessageQueue(queuePath);
            Message msg = new Message();
            try   {
                //Open the file for reading    
                using (FileStream fs = File.OpenRead(fileName))    {
                    // while there are bytes     
                    while ((i = fs.ReadByte()) != -1)     {
                        // if count has reached size, send message      
                        if (count >= chunkSize)      {
                            msgNumber++;
                            msg.AppSpecific = msgNumber;
                            //Send the messsage       
                            queue.Send(msg);
                            string nextMsgId = msg.Id;
                            count = 0;
                            //Create a new message 
                            msg = new Message();
                            msg.CorrelationId = nextMsgId;
                        }
                        msg.BodyStream.WriteByte((byte)i);
                        count++;
                        // from the original file  
                    }
                    msgNumber++;
                    msg.AppSpecific = msgNumber;
                    //Send the last message  
                    queue.Send(msg);
                }
            }
            catch (Exception ex)   {
                Console.WriteLine(ex);
            }
            finally   {
                //release queue resources 
                queue.Close();   }
        }

        static void ReceiveFile(string fileName, string queuePath)
        {
            byte[] data;
            int length;
            //Open an existing queue  
            MessageQueue queue = new MessageQueue(queuePath);
            try   {
                //Open file for writing    
                using (FileStream fs = File.OpenWrite(fileName))   {
                    //Receive the first message     
                    Message msg = queue.Receive(new TimeSpan(0, 0, 0, 1));
                    while (msg != null)     {
                        //Get the Lenght of the message body stream    
                        length = Convert.ToInt32(msg.BodyStream.Length);
                        //Create a buffer to hold the stream in memory     
                        data = new byte[length];
                        //Read the body stream    
                        msg.BodyStream.Read(data, 0, length);
                        //Write the buffer into the file    
                        fs.Write(data, 0, length);
                        //Receive following message   
                        msg = queue.Receive(new TimeSpan(0, 0, 0, 1));
                    }
                }
            }
            catch (Exception ex)   {
                Console.WriteLine(ex);
            }
            finally   {
                //release queue resources    queue.Close(); 
            }
        }
    }
}
2

К сожалению, никак. Это архитектурное ограничение. Подробности тут.

Хотя для BizTalk, который использует тот же MSMQ есть расширение, которое называется BizTalk Message Queuing Large Message Extension. Тут можно почитать подробнее.

Второй вариант - вручную разбивать сообщения на части. Думаю, пример реализации не составит труда нагуглить, т.к тема достаточно избита.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.