使用 ServiceBusTrigger 的 Azure 函数在 Xamarin.Android 应用程序中不起作用

Azure Function using ServiceBusTrigger doesn't work in Xamarin.Android app

我的 Android 应用程序调用我的 Azure 函数时没有触发。

测试:

以下测试通过 ServiceBusTrigger 成功触发了 Azure 功能:

[<Fact>]
let ``Publish message assigned to servicebus topic``() =

    // Setup
    let connectionstring = ConfigurationManager.ConnectionStrings.["servicebus_testEnv"].ConnectionString

    // Test
    async {

        let client  = TopicClient(connectionstring, "Topic.courier-subscribed")
        let data    = "test_data"
        let message = Message(Encoding.UTF8.GetBytes(data))

        do! client.SendAsync(message) |> Async.AwaitTask
        do! client.CloseAsync()       |> Async.AwaitTask
    }

问题:

但是,当我将代码插入我的 Xamarin Android 应用程序时,Azure 功能永远不会被触发。

相反,我只观察到以下错误:

System.TimeoutException: 'The operation did not complete within the allocated time 00:00:49.9480520 for object session5.'

Xamarin.Android:

这是我的 Xamarin Android 应用程序中的代码:

let subscribe : Subscribe =
    fun request ->

        async {

            let body = {
                Courier   = request.Courier
                Location  = request.Location
                Timestamp = DateTime.Now
            }

            try
                let connectionstring = QueueTopic.Instance.ConnectionString
                let client  = TopicClient(connectionstring, "Topic.courier-subscribed")
                let json    = JsonConvert.SerializeObject(body)
                let message = Message(Encoding.UTF8.GetBytes(json))

                do! client.SendAsync(message) |> Async.AwaitTask
                do! client.CloseAsync()       |> Async.AwaitTask

                return Ok body
         }

Azure 函数(ServiceBusTrigger):

public static class SubscribedFn
{
    [FunctionName(nameof(SubscribedFn))]
    public static async Task Run([ServiceBusTrigger("Topic.courier-subscribed", "Subscription.all-messages", Connection= "ServiceBusConnectionKey")]
                                  string json, ILogger log)
    {
        ... // NEVER INVOKED
    }

注意:

  1. 我已验证连接字符串与我的自动化测试相同。

  2. 自动化测试每次通过ServiceBusTrigger触发Azure函数

  3. 我已验证我正在发送 JSON 作为两个客户端的有效负载

您的设备可能存在网络问题。 ServiceBus 默认使用 AMQP 协议。在大多数情况下,您需要在传出连接上打开端口 5671 和 5672。

对于您的 Xamarin 应用程序,我建议将 ;TransportType=AmqpWebSockets 附加到您的连接字符串,看看是否有帮助。

Endpoint=sb://xxxx.servicebus.windows.net/;SharedAccessKeyName=xxxx;SharedAccessKey=xxxxx;TransportType=AmqpWebSockets