您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》


//
// 摘要:
// 在指定的路径中创建非事务性“消息队列”队列。
//
// 参数:
// path:
// 要创建的队列的路径。
//
// 返回结果:
// 表示新队列的 System.Messaging.MessageQueue。
public static MessageQueue Create(string path);
//
// 摘要:
// 在指定的路径中创建事务性或非事务性“消息队列”队列。
//
// 参数:
// transactional:
// 如果创建事务性队列,为 true;如果创建非事务性队列,则为 false。
//
// path:
// 要创建的队列的路径。
//
// 返回结果:
// 表示新队列的 System.Messaging.MessageQueue。
public static MessageQueue Create(string path, bool transactional);
MessageQueue.Create(@".\private$\myQueue");
public XmlMessageFormatter();
public XmlMessageFormatter(string[] targetTypeNames);
public XmlMessageFormatter(Type[] targetTypes);
//序列化为字符串
XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[]
{ typeof(string) });
/**//// <summary>
/// 通过Create方法创建使用指定路径的新消息队列
/// </summary>
/// <param name="queuePath"></param>
public static void Createqueue(string queuePath)

{
try
{
if (!MessageQueue.Exists(queuePath))
{
MessageQueue.Create(@".\private$\myQueue");
}
else
{
Console.WriteLine(queuePath + "已经存在!");
}
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
} 
/**//// <summary>
/// 连接消息队列并发送消息到队列
/// </summary>
public static void SendMessage()

{
try
{
//连接到本地的队列
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
Message myMessage = new Message();
myMessage.Body = "消息内容";
myMessage.Formatter = new XmlMessageFormatter(new Type[]
{ typeof(string) });
//发送消息到队列中
myQueue.Send(myMessage);
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
} 
/**//// <summary>
/// 连接消息队列并从队列中接收消息
/// </summary>
public static void ReceiveMessage()

{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
myQueue.Formatter = new XmlMessageFormatter(new Type[]
{ typeof(string) });
try
{
//从队列中接收消息
Message myMessage = myQueue.Receive();
string context = (string)myMessage.Body; //获取消息的内容
Console.WriteLine("消息内容为:" + context);
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
catch (InvalidCastException e)
{
Console.WriteLine(e.Message);
}
} 
/**//// <summary>
/// 清空指定队列的消息
/// </summary>
public static void ClearMessage()

{
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
myQueue.Purge();
} 
/**//// <summary>
/// 连接队列并获取队列的全部消息
/// </summary>
public static void GetAllMessage()

{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
Message[] message = myQueue.GetAllMessages();
XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[]
{ typeof(string) });
for (int i = 0; i < message.Length; i++)
{
message[i].Formatter = formatter;
Console.WriteLine(message[i].Body.ToString());
}
} 
本实例完整代码免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。