从Xamarin高效订阅服务总线队列?

问题描述:

订阅服务总线队列以接收来自Xamarin应用程序的消息时收到通知的方式是否有任何方法(如果有的话,什么是最佳方法)?从Xamarin高效订阅服务总线队列?

我在一个控制台应用程序中使用此示例,它工作得很好,但不似乎能够实例化一个QueueClient在我Xamarin窗体PCL desipite增加了WindowsAzure.ServiceBug Nuget package参考,没有任何问题:

Console.WriteLine("Receive critical messages. Ctrl-C to exit.\n"); 
var connectionString = "{service bus listen string}"; 
var queueName = "{queue name}"; 

var client = QueueClient.CreateFromConnectionString(connectionString, queueName); 

client.OnMessage(message => 
    { 
     Stream stream = message.GetBody<Stream>(); 
     StreamReader reader = new StreamReader(stream, Encoding.ASCII); 
     string s = reader.ReadToEnd(); 
     Console.WriteLine(String.Format("Message body: {0}", s)); 
    }); 

Console.ReadLine(); 

在Xamarin中有这样做的首选方法吗?

编辑

要清楚,我不想邮件投递到队列,但订阅它,会出现一条消息时有效地接收通知。我可以使用REST Api,但是我必须不断查询更新,这似乎效率很低。

编辑2

我已经使用长轮询尝试,但有一个服务总线队列作为调查返回并没有真正的工作几乎立即以“204无内容”这意味着我仍然不断排队。使用SDK我只能等待消息,并通过我的订阅得到通知

感谢

+1

的[从Xamarin Azure的ServiceBus窗体PCL]可能的复制(HTTP://计算器.com/questions/39753431/azure-servicebus-from-xamarin-forms-pcl) – Thomas

+0

谢谢@Thomas,但我不想将消息推送到队列中。我想以最有效的方式进行订阅,并且理想情况下无需从客户端连续轮询服务总线队列 – LDJ

如果我理解正确的,你提到的是不适合的Xamarin平台的SDK。请尝试使用Microsoft.Azure.ServiceBus。这是一个预发售版本。

如果我们想OT不断地轮询来自客户端的服务总线队列,请试试用下面的代码:

var queueClient = new QueueClient(connectionstring, queueName, ReceiveMode.PeekLock); 

    // Register a OnMessage callback 
    queueClient.RegisterMessageHandler(
     async (message, token) => 
     { 
      // Process the message 
      string messageBody = Encoding.UTF8.GetString(message.Body); 

      // Complete the message so that it is not received again. 
      // This can be done only if the queueClient is opened in ReceiveMode.PeekLock mode. 
      await queueClient.CompleteAsync(message.SystemProperties.LockToken); 
       },new MessageHandlerOptions 
       { 
        MaxConcurrentCalls = 1, AutoComplete = false 
       });