Response.Write("成功发送消息,"+DateTime.Now+"<br/>");
}
private void receiveComplexMsg()
{
MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
//调用MessageQueue的Receive方法接收消息
if (MQ.GetAllMessages().Length > 0)
{
System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));
if (message != null)
{
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(MsgModel) });//消息类型转换
MsgModel msg = (MsgModel)message.Body;
Response.Write(string.Format("接收消息成功,lable:{0},body:{1},{2}<br/>", message.Label, msg, DateTime.Now));
}
}
else
{
Response.Write("没有消息了!<br/>");
}
}
private T receiveComplexMsg<T>()
{
MessageQueue MQ = new MessageQueue(@".\private$\MsgQueue");
//调用MessageQueue的Receive方法接收消息
if (MQ.GetAllMessages().Length > 0)
{
System.Messaging.Message message = MQ.Receive(TimeSpan.FromSeconds(5));
if (message != null)
{
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(T) });//消息类型转换
T msg = (T)message.Body;
return msg;
}
}
return default(T);
}