服务总线2.1对于Windows Server - EndReceive方法的TimeOutException

 My_Qzj 发布于 2023-02-14 01:39

我正在使用Service Bus 2.1对于Windows Server,我有一种方法来异步接收消息.

我方法的主体是:

var waitTimeout = TimeSpan.FromSeconds(10);

        // Declare an action acting as a callback whenever a message arrives on a queue.
        AsyncCallback completeReceive = null;

        // Declare an action acting as a callback whenever a non-transient exception occurs while receiving or processing messages.
        Action recoverReceive = null;

        // Declare an action responsible for the core operations in the message receive loop.
        Action receiveMessage = () =>
        {
            // Use a retry policy to execute the Receive action in an asynchronous and reliable fashion.
            retryPolicy.ExecuteAction(
                (cb) => messageReceiver.BeginReceive(waitTimeout, cb, null),
                (ar) =>
                    {
                        // Make sure we are not told to stop receiving while we were waiting for a new message.
                        if (!cts.IsCancellationRequested)
                        {
                            // Complete the asynchronous operation. This may throw an exception that will be handled internally by retry policy.
                            BrokeredMessage msg = messageReceiver.EndReceive(ar);

                            // Check if we actually received any messages.
                            if (msg != null)
                            {
                                // Make sure we are not told to stop receiving while we were waiting for a new message.
                                if (!cts.IsCancellationRequested)
                                {
                                    try
                                    {
                                        // Process the received message.
                                        processMessage(msg);

                                        // With PeekLock mode, we should mark the processed message as completed.
                                        if (messageReceiver.Mode == ReceiveMode.PeekLock)
                                        {
                                            // Mark brokered message as completed at which point it's removed from the queue.
                                            msg.SafeComplete();
                                        }
                                    }
                                    catch
                                    {
                                        // With PeekLock mode, we should mark the failed message as abandoned.
                                        if (messageReceiver.Mode == ReceiveMode.PeekLock)
                                        {
                                            // Abandons a brokered message. This will cause Service Bus to unlock the message and make it available 
                                            // to be received again, either by the same consumer or by another completing consumer.
                                            msg.SafeAbandon();
                                        }

                                        // Re-throw the exception so that we can report it in the fault handler.
                                        throw;
                                    }
                                    finally
                                    {
                                        // Ensure that any resources allocated by a BrokeredMessage instance are released.
                                        msg.Dispose();
                                    }
                                }
                                else
                                {
                                    // If we were told to stop processing, the current message needs to be unlocked and return back to the queue.
                                    if (messageReceiver.Mode == ReceiveMode.PeekLock)
                                    {
                                        msg.SafeAbandon();
                                    }
                                }
                            }
                        }

                        // Invoke a custom callback method to indicate that we have completed an iteration in the message receive loop.
                        completeReceive(ar);
                    },
                () =>
                    {

                    },
                (ex) =>
                {
                    // Invoke a custom action to indicate that we have encountered an exception and
                    // need further decision as to whether to continue receiving messages.
                    recoverReceive(ex);
                });
        };

        // Initialize a custom action acting as a callback whenever a message arrives on a queue.
        completeReceive = (ar) =>
        {
            if (!cts.IsCancellationRequested)
            {
                // Continue receiving and processing new messages until we are told to stop.
                receiveMessage();
            }
        };

        // Initialize a custom action acting as a callback whenever a non-transient exception occurs while receiving or processing messages.
        recoverReceive = (ex) =>
        {
            if (!cts.IsCancellationRequested)
            {
                // Continue receiving and processing new messages until we are told to stop regardless of any exceptions.
                receiveMessage();
            }
        };

        // Start receiving messages asynchronously.
        receiveMessage();

由于我已经将dll Microsoft.ServiceBus更新到版本2.1(在我使用版本1.8和2.0之前),我每秒都有两个例外:

Microsoft.ServiceBus.dll中出现'System.ServiceModel.FaultException`1'类型的第一次机会异常Microsoft.ServiceBus.dll中出现类型'System.TimeoutException'的第一次机会异常

引发异常的方法有:'EndReceive'和Microsoft.ServiceBus.Messaging.Sbmp.DuplexRequestBindingElement.DuplexRequestSessionChannel.ThrowIfFaultMessage(消息wcfMessage)

我在互联网上看过一些帖子,但没有任何答案.有人已经遇到过这个问题吗?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有