msmq复杂格式消息发送和接收? 各位大虾,我想请教一下。net下利用msmq来发送和接收复杂消息的实现,比如我发送一个xml文件,在接收方应该如何实现接收,或则是二进制文件的发送和接收,请帮忙,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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()); } } 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(); } 如何 自定义 winForm 控件? 关于窗体调用的问题 IIS 关于主线程和子线程 treeView 展开结点 请求帮助 这个自定义图片功能怎么实现 如何为combobox增加一项 求一个小的Demo程序,关于使用WPF Listview控件的 如何用反射获取结构体内的数据 IIS下运行C#程序的问题 DataSet提交到数据库
{
//本地消息发送 二进制格式文件传输
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());
}
}
{
// 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();
}