当我的 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
}
注意:
我已验证连接字符串与我的自动化测试相同。
自动化测试每次都会通过ServiceBusTrigger触发Azure功能。
我已验证我正在两个客户端中发送 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/