Using Reactive extension (Rx) for MSMQ message receive using async pattern (queue.BeginReceive,queue.EndReceive)

别说谁变了你拦得住时间么 提交于 2019-11-30 03:20:28

问题


I have been using Rx for a while now for Events on my projects and dedicatedly for Socket programming and the good part is its doing well. Managing my code, performance advantage and much better to execute and interpret.

Lately I have to modify my project's process flow where i need to dump all the incoming data (from socket operations) into queues (using MSMQ implementation as decided for queueing).

As MSMQ provides async call for dequeing messages from the queue (but in an wierd pattern). I have been struggling to use Rx for this purpose now, but enable to do so.

Question : Can some one give me a clean code example to implement Rx for message receiving from queue using Async pattern.

I need the async operator implementation for MSMQ analogous to something like this

var data = Observable.FromAsyncPattern<byte[]>(
                        this.receiverSocket.BeginReceive,
                        this.receiverSocket.EndReceive(some parameters);

Thanks in advance. *cheers* to Rx and .NET


回答1:


It would be as simple as:

var queue = new System.Messaging.MessageQueue("test");
var fun = Observable.FromAsyncPattern((cb, obj) => queue.BeginReceive(TimeSpan.FromMinutes(10),obj,cb), a => queue.EndReceive(a));
var obs = fun();


来源:https://stackoverflow.com/questions/8005892/using-reactive-extension-rx-for-msmq-message-receive-using-async-pattern-queu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!