1.安装MSMQ

2.添加私有的队列

3.MSMQ可以发送的类型可以是任意类型,包括类

        static string strServer = @"FormatName:Direct=TCP:10.7.46.42\private$\msmq";

        static void Main(string[] args)
{
DeleteAllMessage();
SendMessage();
ReveiveMessage();
} public static void DeleteAllMessage()
{
MessageQueue myQueue = new MessageQueue(strServer);
myQueue.Purge(); //删除此队列中包含的所有消息
myQueue.Dispose();
} public static void SendMessage()
{
MessageQueue myQueue = new MessageQueue(strServer);
Message myMessage = new Message();
try
{
myMessage.Body = new Person() { Name = "jake", Age = 29, Birthday = Convert.ToDateTime("1987/07/20") };
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(Person) });
//设置消息发送的优先级别
myMessage.Priority = MessagePriority.Highest; //最高消息优先级
myQueue.Send(myMessage);
}
catch(Exception ex)
{ }
finally
{
myMessage.Dispose();
myQueue.Dispose();
} } public static void ReveiveMessage()
{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(strServer);
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(Person) });
Message myMessage = null;
try
{
int count = myQueue.GetAllMessages().Length;
//从队列中接收消息
//Peek: 返回队列中第一条消息的副本,而不从队列中移除该消息
//Receive: 接收队列中的第一条消息,但不将它从队列中移除
//PeekById: 返回具有指定消息标识符的消息的副本,但不从队列中移除消息
//ReceiveById: 接收匹配给定标识符的消息,并将其从队列中移除
myMessage = myQueue.Receive();
Person p = (Person)myMessage.Body; //获取消息的内容
Console.WriteLine("Name:" + p.Name);
Console.WriteLine("Age:" + p.Age.ToString());
Console.WriteLine("Birthday:" + p.Birthday.ToString("yyyy/MM/dd"));
count = myQueue.GetAllMessages().Length;
Console.ReadLine(); }
catch
{
Console.WriteLine("error4");
}
finally
{
myMessage.Dispose();
myQueue.Dispose();
}
}

  

最新文章

  1. 如何让WEBAPI 能够进行跨越访问
  2. app的推广
  3. checkbox全选,反选,取消选择 jquery
  4. 关于GUID的相关知识
  5. memcached命令行操作详解,命令选项的详细解释
  6. 如何解决div层被flash遮盖的问题
  7. 端口扫描之王——nmap入门精讲(一)
  8. requesting java ast from selection
  9. UIGestureRecognizer手势
  10. java企业架构 spring mvc +mybatis + KafKa+Flume+Zookeeper
  11. Swift json字典转模型 项目记录
  12. Go语言中函数的实现
  13. vagrant极简教程:快速搭建centos7
  14. javaScript之数组操作方法(一)
  15. s21day12 python笔记
  16. Zuul 跨域
  17. 设计模式之观察者模式(Observer)
  18. 【Codeforces 1110D】Jongmah FST分析
  19. SQL 语法笔记
  20. [LeetCode&Python] Problem 824. Goat Latin

热门文章

  1. Visual Studio 如何使用代码片段Code Snippet提高编程速度!!!
  2. CSS大杂烩(1)
  3. UGUI 学习笔记
  4. PAT MOOC dataStructure 4-1
  5. js自动更新时间+星期
  6. NHibernate简单使用介绍
  7. android studio--百度定位集成001
  8. ACM/ICPC 之 ACM计算机工厂-EK算法(POJ3436)
  9. Android之界面(布局文件layput)
  10. jQuery 名称冲突