文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>MSMQ(微软消息队列)异步接收事物队列消息无带事物的重载!(微软的消息队列类库的Bug?!)

MSMQ(微软消息队列)异步接收事物队列消息无带事物的重载!(微软的消息队列类库的Bug?!)

时间:2011-01-21  来源:CopyPaster

异步接收:MessageQueue.BeginReceive()则无带事物的重载:

 

那么,对于支持事物的消息对列,要异步,要带事物,那么如何接收消息呢?

这里给出一种变通方法:先异步BeginPeek(),然后再带事务异步接收Receive()

 

 

代码
 1 /// <summary>
2 /// 异步接收事务消息队列消息
3 /// 异步接收BeginReceive()方法无MessageQueueTransaction重载(微软类库的Bug?)
4 /// 这里变通一下:先异步BeginPeek(),然后带事务异步接收Receive(MessageQueueTransaction)
5 /// </summary>
6 private void AsyncReceiveWithTransactional()
7 {
8 var messageQueue = new MessageQueue(_transactionalQueuePath)
9 {
10 Formatter = new XmlMessageFormatter(new Type[] { typeof(MessageObj) })
11 };
12 messageQueue.PeekCompleted += new PeekCompletedEventHandler(OnPeekCompleted);
13
14 lblBeginTime.Text = DateTime.Now.ToString();
15 using (var messageEnumerator = messageQueue.GetMessageEnumerator2())
16 {
17 while (messageEnumerator.MoveNext())
18 {
19 try
20 {
21 messageQueue.BeginPeek();
22 }
23 catch (Exception ex)
24 {
25 Debug.WriteLine("Exception:ReceiveMessage Exception:" + ex);
26 }
27 }
28 }
29
30 lblEndTime.Text = DateTime.Now.ToString();
31 MessageBox.Show("Receive Done!");
32 }
33
34 private void OnPeekCompleted(object sender, PeekCompletedEventArgs e)
35 {
36 Message message = null;
37 var messageQueueTransaction = new MessageQueueTransaction();
38 try
39 {
40 var messageQueue = (MessageQueue)sender;
41 messageQueue.EndPeek(e.AsyncResult);
42
43 messageQueueTransaction.Begin();
44 message = messageQueue.Receive(messageQueueTransaction);
45 messageQueueTransaction.Commit();
46 }
47 catch (Exception ex)
48 {
49 messageQueueTransaction.Abort();
50 Debug.WriteLine("Exception:ReceiveMessage Exception:" + ex);
51 }
52
53 if (message != null)
54 {
55 var messageObj = (MessageObj)message.Body;
56 Debug.WriteLine("Async Receive Message Done!(" + messageObj.MediaTaskId + ")");
57 }
58 }

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载