首页 > 解决方案 > Xamarin Forms 中的 Azure 服务总线

问题描述

我正在尝试使用 Xamarin 表单订阅 Azure 服务总线队列。(老实说,我不确定是否有可能做到这一点。)

我可以使用简单的控制台应用程序从队列中接收消息,而不会出现任何问题。但是,当我将相同的代码移动到 Xamarin 时,它会在两种不同的情况下失败。

很少,它工作正常,我收到消息(虽然在 1-2 分钟后)但是Microsoft.Azure.ServiceBus.ServiceBusTimeoutException: 'The operation did not complete within the allocated time 00:00:59.9923240 for object dispose.' 当它试图在这一行完成消息时会出错await queueClient.CompleteAsync(message.SystemProperties.LockToken);

其他时候,它甚至没有收到任何消息就失败了,并给出了这个错误The operation did not complete within the allocated time 00:00:56.2875140 for object session5.

这是我的代码:

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        static IQueueClient queueClient;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

            var serviceBusConnectionString = "myConnectionString";
            var queueName = "myqueueName";

            var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler);
            messageHandlerOptions.AutoComplete = false;
            messageHandlerOptions.MaxConcurrentCalls = 1;

            queueClient = new QueueClient(serviceBusConnectionString, queueName);
            queueClient.RegisterMessageHandler(ProcessMessageAsync, messageHandlerOptions);
        }

        private static async Task ProcessMessageAsync(Message message, CancellationToken token)
        {
            var result = Encoding.UTF8.GetString(message.Body);
            await queueClient.CompleteAsync(message.SystemProperties.LockToken);
        }
}

问题:

  1. 是否可以使用 Xamarin 正确订阅 Azure 服务总线队列?
  2. 我在这里错过了什么吗?
  3. 我还有其他选择可以将 JSON 对象从服务总线发送到手机吗?

标签: c#xamarin.formsazureservicebusazure-servicebus-queues

解决方案


根据我的评论,在 Xamarin 应用程序中实现类似的订阅模式时,我确实发现 Signal R 工作得更好,并且实现起来非常容易和轻量级。这是主观的,但我确实发现连接的协商方法比直接连接到 Azure 服务总线端点更简洁


推荐阅读