加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如果我的函数在处理时失败,是否会丢失System.MessageQueue

发布时间:2020-12-15 23:53:09 所属栏目:百科 来源:网络整理
导读:我使用以下代码发送消息: var transaction = new MessageQueueTransaction())transaction.Begin( );var message = new Message{ Body = myContent,Recoverable = true};m_oMessageQueue.Send( message,myTransaction );transaction.Commit( ); 并使用BeginR
我使用以下代码发送消息:

var transaction = new MessageQueueTransaction())
transaction.Begin(  );

var message = new Message
{
   Body = myContent,Recoverable = true
};

m_oMessageQueue.Send( message,myTransaction );

transaction.Commit( );

并使用BeginRecieve和ReceiveCompleted事件处理程序接收它.

如果我的事件处理程序在调用EndRecieve之前失败,那么该消息是否应保留在队列中并可用于后续的接收调用?我看到的行为是消息永远消失了. (或者可能会有超时,之后它会再次出现?)

更新接收消息的代码如下所示.

var messageQueue = new MessageQueue( myPath );
messageQueue.ReceiveCompleted += messageQueue_ReceiveCompleted_ThrowException;
messageQueue.BeginReceive();

出于测试目的,我在messageQueue_ReceiveCompleted_ThrowException事件处理程序中抛出异常.

然后我用一个工作事件处理程序重复上面的代码,但我没有被调用.

解决方法

看起来问题在于您将BeginReceive与事务队列一起使用. From MSDN:

Note Do not use the asynchronous
call BeginReceive with transactions.
If you want to perform a transactional
asynchronous operation,call
BeginPeek,and put the transaction and
the (synchronous) Receive method
within the event handler you create
for the peek operation. Your event
handler might contain functionality as
shown in the following C# code.

对于这样的失败,我认为消息通常会进入死信队列或产生否定确认,具体取决于您配置MessageQueue的方式(这可以解释为什么您没有看到它们).你可以read more about those options here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读