首页 > 解决方案 > 在 Xamarin Forms 中网络断开后 Azure 服务总线 QueueClient 未重新连接

问题描述

当我断开网络连接时,当网络连接恢复时,QueueClient 将不会重新连接回 Azure 服务总线。从那时起,所有消息都留在服务器上。

我尝试更改 RetryPolicy 没有效果。我可以让它重新连接的唯一方法是重新创建一个新的 QueueClient。客户端代码基于 Microsoft 示例。

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
using Utils;
using WeakEvent;
using Message = Microsoft.Azure.ServiceBus.Message;

namespace Messaging
{
public class QueueClient
{
    private readonly Microsoft.Azure.ServiceBus.QueueClient Client;

    private readonly WeakEventSource<string> _OnReceived = new WeakEventSource<string>();


    public event EventHandler<string> OnReceived
    {
        add => _OnReceived.Subscribe( value );
        remove => _OnReceived.Unsubscribe( value );
    }


    public QueueClient( AzureClient client )
    {
        var Keys = client.RequestMessagingConnect( new MessagingRequest
                                                   {
                                                       ConnectionType = MessagingRequest.MESSAGING_CONNECTION_TYPE.DRIVER
                                                   } ).Result;

        if( Keys.Connected )
        {
            var Pk = Encryption.Decrypt( Keys.Pk );
            var Id = Encryption.Decrypt( Keys.Id );

            Client = new Microsoft.Azure.ServiceBus.QueueClient( Pk, Id, retryPolicy: new RetryExponential( new TimeSpan( 0, 0, 10 ), new TimeSpan( 0, 0, 30 ), int.MaxValue ) );

            // Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
            var MessageHandlerOptions = new MessageHandlerOptions( ExceptionHandler.ExceptionReceivedHandler )
                                        {
                                            // Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
                                            // Set it according to how many messages the application wants to process in parallel.
                                            MaxConcurrentCalls = 1,

                                            // Indicates whether the message pump should automatically complete the messages after returning from user callback.
                                            // False below indicates the complete operation is handled by the user callback as in ProcessMessagesAsync().
                                            AutoComplete = false
                                        };

            // Register the function that processes messages.
            Client.RegisterMessageHandler( ProcessMessagesAsync, MessageHandlerOptions );
        }
    }

    private async Task ProcessMessagesAsync( Message message, CancellationToken token )
    {
        // Process the message.
        var Body = Encoding.UTF8.GetString( message.Body );

        var Message = Protocol.Data.Message.FromJson( Body );

            _OnReceived?.Raise( this, Message ) );

        // Complete the message so that it is not received again.
        // This can be done only if the queue Client is created in ReceiveMode.PeekLock mode (which is the default).
        await Client.CompleteAsync( message.SystemProperties.LockToken );

        // Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.
        // If queueClient has already been closed, you can choose to not call CompleteAsync() or AbandonAsync() etc.
        // to avoid unnecessary exceptions.
    }
}

}

标签: c#networkingxamarin.formsazureservicebus

解决方案


推荐阅读