c# - 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);
}
}
问题:
- 是否可以使用 Xamarin 正确订阅 Azure 服务总线队列?
- 我在这里错过了什么吗?
- 我还有其他选择可以将 JSON 对象从服务总线发送到手机吗?
解决方案
根据我的评论,在 Xamarin 应用程序中实现类似的订阅模式时,我确实发现 Signal R 工作得更好,并且实现起来非常容易和轻量级。这是主观的,但我确实发现连接的协商方法比直接连接到 Azure 服务总线端点更简洁
推荐阅读
- javascript - 请解释查找数组深度的递归解决方案
- google-apps-script - 我们可以在电子表格的 Google 应用脚本中调用函数“显示公式”吗
- javascript - 如何使用另一个类作为定义的数据类型来验证嵌套 json 的模式结构
- flutter - 我如何在颤振上使用动物数据库?
- xaml - Xamarin 跨平台应用程序重定向到 main.storyboard
- r - 闪亮的仪表板 - 在服务器生成的侧边栏 UI 中隐藏菜单项
- flutter - 在颤振中验证之前清理 TextFormField 值
- angular - Tyring 从 '@angular/common/http' 导入 import { HttpClient, Headers, RequestOptions };
- swift - 使用 AVPlayer 返回“非多路径连接”错误
- python-3.x - 如何保存下一个算法的输出