各位大虾,我想请教一下。net下利用msmq来发送和接收复杂消息的实现,比如我发送一个xml文件,在接收方应该如何实现接收,或则是二进制文件的发送和接收,请帮忙,谢谢!

解决方案 »

  1.   

    private void local_msg_send_Click(object sender, EventArgs e)
            {
               //本地消息发送 二进制格式文件传输
                CreateQueue(@".\private$\TSS_MsmqTest");
                CreateQueue(@".\private$\TSS_MsmqTest_QueueAdministration");
                try
                {
                    this.localMessageQueue = new MessageQueue(local_mq.Text);
                    
                    System.IO.FileStream inFile = new FileStream("C:\\zhouxingchi.mp3", System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    byte[] binaryData = new byte[inFile.Length];
                    inFile.Read(binaryData, 0, (int)inFile.Length);
                    string mStr = Convert.ToBase64String(binaryData);
                    string hh = mStr;                System.Messaging.Message Msg = new System.Messaging.Message(binaryData, new BinaryMessageFormatter());
                    //使用BinaryMessageFormatter这个类型进行串行化,默认情况下,这个属性不进行赋值的话就是XmlMessageFormatter
                    //System.Messaging.Message Msg = new System.Messaging.Message();
                    // Msg.Formatter = new BinaryMessageFormatter();
                    Msg.AdministrationQueue = new MessageQueue(@".\private$\TSS_MsmqTest_QueueAdministration");
                    Msg.AcknowledgeType = AcknowledgeTypes.PositiveReceive | AcknowledgeTypes.PositiveArrival;
                    Msg.Label = "[mp3音乐]";
                    //Msg.Body = mStr;
                    localMessageQueue.Send(Msg);
                    localMessageQueue.Close();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                } 
                
            }
      

  2.   

            public string ReceiveMessage(string mq_path)
            {
                // Connect to the a queue on the local computer.
                //MessageQueue myQueue = new MessageQueue(@".\private$\TSS_MsmqTest");
                //this.localMessageQueue = new MessageQueue(local_mq.Text);
                this.localMessageQueue = new MessageQueue(mq_path);
                this.localMessageQueue.MessageReadPropertyFilter.CorrelationId = true;
                byte[] bytes = new byte[1024 * 1024 * 4];
                System.Messaging.Message Msg = new System.Messaging.Message(bytes, new BinaryMessageFormatter());
                //使用BinaryMessageFormatter这个类型进行串行化,默认情况下,这个属性不进行赋值的话就是XmlMessageFormatter
                string returnString = null;            try
                {
                    Msg = localMessageQueue.Receive();
                    IFormatter bf = new BinaryFormatter();
                    object obj = bf.Deserialize(Msg.BodyStream);
                    bytes = (byte[])obj;
                    //bytes= (byte[])Msg.Body;
                    //byte[] bytes = Convert.FromBase64String(pic);//声明一个byte[]用来存放Base64解码转换过来的数据流
                    //byte[] bytes = (byte[])Msg.Body;
                    //声明一个byte[]用来存放数据流
                    //创建文件流并保存
                    FileStream outfile = new System.IO.FileStream("C:\\zhou_xingchi.mp3", System.IO.FileMode.Create,FileAccess.Write);
                    outfile.Write(bytes, 0, (int)bytes.Length);
                    returnString = Msg.Id;
                  }
                  catch (MessageQueueException ex)
                  {
                      // Handle Message Queuing exceptions.
                      MessageBox.Show(ex.Message);
                  }                // Handle invalid serialization format.
                  catch (InvalidOperationException e)
                  {
                     MessageBox.Show(e.Message);
                  }              // Catch other exceptions as necessary.            return returnString;
                //this.localMessageQueue.Close();
            }