OrderMessage 为自定义的class写函数
OrderMessage om= new OrderMessage();
System.Messaging.Message ms = new System.Messaging.Message();
om.Order_Id = 1;
om.Order_Customer_Name = "sssss";
ms.Body = om;
MSMQ.Send(ms);
读函数
OrderMessage om = new OrderMessage();
System.Messaging.Message[] ms;
System.Messaging.XmlMessageFormatter stringFormatter;
stringFormatter = new System.Messaging.XmlMessageFormatter(new string[] {"OrderMessage"});ms = MSMQ.GetAllMessages();
for (int index = 0; index < ms.Length; index++)
{
ms[index].Formatter = stringFormatter;
om = (OrderMessage)ms[index].Body; <-------出错!
}
OrderMessage om= new OrderMessage();
System.Messaging.Message ms = new System.Messaging.Message();
om.Order_Id = 1;
om.Order_Customer_Name = "sssss";
ms.Body = om;
MSMQ.Send(ms);
读函数
OrderMessage om = new OrderMessage();
System.Messaging.Message[] ms;
System.Messaging.XmlMessageFormatter stringFormatter;
stringFormatter = new System.Messaging.XmlMessageFormatter(new string[] {"OrderMessage"});ms = MSMQ.GetAllMessages();
for (int index = 0; index < ms.Length; index++)
{
ms[index].Formatter = stringFormatter;
om = (OrderMessage)ms[index].Body; <-------出错!
}
解决方案 »
- C# Tcp客户端与服务端通信问题
- 如何才能学好c
- c# GetOleDbSchemaTable获取Sheet名称不一致?
- 用TextBox往数据库添加数据 DataGrid显示出来的问题(数据添加进去显示不出来 郁闷)谁帮看一下(附代码)
- PropertyGrid (...)按钮的问题 (PropertyGrid 's (...)Button)
- 初学者:关于C#的一点基础概念的问题.顶者有分.望各位不惜赐教,谢谢!
- [WPF] 如何在自己定义的style里动态绑定图片
- 如果数据被绑定到控件之后,修改控件上的数据会同时修改数据库里的数据吗?
- 在十个Button中我想判断那个Buttont.ID=aaa怎么写呀
- C#在窗体上画线,并且当线条穿过窗体上控件时,也不被控件覆盖
- jmail
- 问了一个问题,没有一个人回贴:( 分只能散了,回贴给分.
/// <summary>
/// This Class is used read and write message from MessageQueue
/// </summary>
public class Cls_MessageMG
{
public static void clear_message(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
}
MessageQueue mq=new MessageQueue(Queue_Path);
mq.Purge();
} public static void send_message(string Queue_Path, string Msg_Content, Msg_Type Msg_Type_Instance)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
}
MessageQueue mq=new MessageQueue(Queue_Path);
mq.Label=System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff");
switch (Msg_Type_Instance)
{
case Msg_Type.XML:
mq.Formatter=new XmlMessageFormatter();
break;
case Msg_Type.Binary:
mq.Formatter=new BinaryMessageFormatter();
break;
case Msg_Type.ActiveX:
mq.Formatter=new ActiveXMessageFormatter();
break;
}
mq.Send(Msg_Content);
} public static string receive_message(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return "";
} MessageQueue mq= new MessageQueue(Queue_Path);
mq.Label=System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff");
((XmlMessageFormatter)mq.Formatter).TargetTypeNames=new string[]{"System.String,mscorlib"}; try
{
System.Messaging.Message m = mq.Receive();
return (string)m.Body;
}
catch (Exception ex)
{
return (ex.ToString());
}
} public static string peek_message(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return "";
} MessageQueue mq= new MessageQueue(Queue_Path);
mq.Label=System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff");
((XmlMessageFormatter)mq.Formatter).TargetTypeNames=new string[]{"System.String,mscorlib"}; try
{
System.Messaging.Message m = mq.Peek();
return (string)m.Body;
}
catch (Exception ex)
{
return (ex.ToString());
}
} public static bool IsQueueEmpty(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
bool isQueueEmpty = false; if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return true;
} // Connect to a queue.
MessageQueue myQueue = new MessageQueue(Queue_Path); try
{
// Set Peek to return immediately.
myQueue.Peek(new TimeSpan(0)); // If an IOTimeout was not thrown, there is a message
// in the queue.
isQueueEmpty = false;
} catch(MessageQueueException ex)
{
if (ex.MessageQueueErrorCode ==
MessageQueueErrorCode.IOTimeout)
{
// No message was in the queue.
isQueueEmpty = true;
} // Handle other sources of MessageQueueException.
} // Handle other exceptions as necessary. // Return true if there are no messages in the queue.
return isQueueEmpty;
}
public static int GetQueueCount(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path; if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return 0;
} MessageQueue myQueue = new MessageQueue(Queue_Path);
MessageEnumerator myEnumerator = myQueue.GetMessageEnumerator(); int message_count=0;
while (myEnumerator.MoveNext())
{
message_count++;
}
return message_count;
} }
#endregion
public enum Msg_Type {XML, Binary, ActiveX};