7

Windows Server 用の Service Bus 2.1 を使用しており、メッセージを非同期的に受信する方法があります。

私のメソッドの本体は次のとおりです。

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<Exception> 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 を使用する前に)、毎秒 2 つの例外があります。

タイプ 'System.ServiceModel.FaultException`1' の初回例外が Microsoft.ServiceBus.dll で発生しました タイプ 'System.TimeoutException' の初回例外が Microsoft.ServiceBus.dll で発生しました

例外を発生させるメソッドは次のとおりです。「EndReceive」および Microsoft.ServiceBus.Messaging.Sbmp.DuplexRequestBindingElement.DuplexRequestSessionChannel.ThrowIfFaultMessage(Message wcfMessage)

インターネットでいくつかの投稿を見ましたが、答えはありません。誰かがすでにこの問題を抱えていますか?

4

1 に答える 1

1

Servicebus ライブラリのバージョン 2.05 でも同じ問題が発生しました。3.0にアップデートしたところ、問題は解決しました。

于 2015-09-02T08:38:23.097 に答える