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

标签 azure xamarin.android azure-functions azureservicebus

当我的 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协议(protocol)。大多数情况下,您需要在传出连接上打开端口 5671 和 5672。

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

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

关于使用 ServiceBusTrigger 的 Azure 函数在 Xamarin.Android 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59909389/

相关文章:

Azure Web 服务选项为 "Legacy"

Azure 应用程序无法启动并收到 HTTP 500 错误

c# - 强制用户更新到最新版本

c# - 如何从 c# Xamarin android 中的浏览器链接打开应用程序?

azure-functions - Azure 函数与 Cosmos DB 绑定(bind)返回格式化的日期时间?

在 Terraform 中使用 azurerm_runction_app_function 时,Azure 函数 Url 不包含身份验证代码

node.js - 在 Azure 应用服务中部署前端和后端应用程序的最佳方法是什么?

azure - 使用 Azure 数据工厂将压缩的 XML 文件从 HTTP 链接源复制并提取到 Azure Blob 存储

c# - 在 Xamarin.Forms 中编写特定于设备平台的代码

c# - 右键单击部署时如何运行 --publish-local-settings?