Azure Function App:无法将队列绑定到类型 'Microsoft.WindowsAzure.Storage.Queue.CloudQueue' (IBinder)
Azure Function App: Can't bind Queue to type 'Microsoft.WindowsAzure.Storage.Queue.CloudQueue' (IBinder)
我的 Azure 函数方案:
- HTTP 触发器。
- 基于 HTTP 参数,我想从适当的存储队列中读取消息并 return 返回数据。
这里是函数的代码 (F#):
let Run(request: string, customerId: int, userName: string, binder: IBinder) =
let subscriberKey = sprintf "%i-%s" customerId userName
let attribute = new QueueAttribute(subscriberKey)
let queue = binder.Bind<CloudQueue>(attribute)
() //TODO: read messages from the queue
编译成功(使用正确的 NuGet 引用和打开包),但我得到运行时异常:
Microsoft.Azure.WebJobs.Host:
Can't bind Queue to type 'Microsoft.WindowsAzure.Storage.Queue.CloudQueue'.
我的代码基于 this article 中的示例。
我做错了什么?
更新:现在我意识到我没有在任何地方指定连接名称。基于 IBinder
的队列访问是否需要绑定?
更新 2:我的 function.json
文件:
{
"bindings": [
{
"type": "httpTrigger",
"name": "request",
"route": "retrieve/{customerId}/{userName}",
"authLevel": "function",
"methods": [
"get"
],
"direction": "in"
}
],
"disabled": false
}
我怀疑您遇到了版本控制问题,因为您引入了有冲突的 Storage SDK 版本。相反,使用内置的(w/o 引入任何 nuget 包)。此代码适用于无 project.json:
#r "Microsoft.WindowsAzure.Storage"
open Microsoft.Azure.WebJobs;
open Microsoft.WindowsAzure.Storage.Queue;
let Run(request: string, customerId: int, userName: string, binder: IBinder) =
async {
let subscriberKey = sprintf "%i-%s" customerId userName
let attribute = new QueueAttribute(subscriberKey)
let! queue = binder.BindAsync<CloudQueue>(attribute) |> Async.AwaitTask
() //TODO: read messages from the queue
} |> Async.RunSynchronously
这将绑定到 默认存储帐户(我们在创建 Function App 时为您创建的帐户)。如果您想指向不同的存储帐户,您需要创建一个 属性数组 ,并包含一个 StorageAccountAttribute
指向您想要的存储帐户(例如 new StorageAccountAttribute("your_storage")
).然后,您将此属性数组(队列属性位于数组的第一个)传递到采用属性数组的 BindAsync
重载。有关详细信息,请参阅 。
但是,如果您不需要做任何复杂的事情 parsing/formatting 来形成队列名称,我认为您甚至不需要为此使用 Binder.您可以完全以声明方式绑定到队列。这是 function.json 和代码:
{
"bindings": [
{
"type": "httpTrigger",
"name": "request",
"route": "retrieve/{customerId}/{userName}",
"authLevel": "function",
"methods": [
"get"
],
"direction": "in"
},
{
"type": "queue",
"name": "queue",
"queueName": "{customerId}-{userName}",
"connection": "<your_storage>",
"direction": "in"
}
]
}
以及函数代码:
#r "Microsoft.WindowsAzure.Storage"
open Microsoft.Azure.WebJobs;
open Microsoft.WindowsAzure.Storage.Queue;
let Run(request: string, queue: CloudQueue) =
async {
() //TODO: read messages from the queue
} |> Async.RunSynchronously
我的 Azure 函数方案:
- HTTP 触发器。
- 基于 HTTP 参数,我想从适当的存储队列中读取消息并 return 返回数据。
这里是函数的代码 (F#):
let Run(request: string, customerId: int, userName: string, binder: IBinder) =
let subscriberKey = sprintf "%i-%s" customerId userName
let attribute = new QueueAttribute(subscriberKey)
let queue = binder.Bind<CloudQueue>(attribute)
() //TODO: read messages from the queue
编译成功(使用正确的 NuGet 引用和打开包),但我得到运行时异常:
Microsoft.Azure.WebJobs.Host:
Can't bind Queue to type 'Microsoft.WindowsAzure.Storage.Queue.CloudQueue'.
我的代码基于 this article 中的示例。
我做错了什么?
更新:现在我意识到我没有在任何地方指定连接名称。基于 IBinder
的队列访问是否需要绑定?
更新 2:我的 function.json
文件:
{
"bindings": [
{
"type": "httpTrigger",
"name": "request",
"route": "retrieve/{customerId}/{userName}",
"authLevel": "function",
"methods": [
"get"
],
"direction": "in"
}
],
"disabled": false
}
我怀疑您遇到了版本控制问题,因为您引入了有冲突的 Storage SDK 版本。相反,使用内置的(w/o 引入任何 nuget 包)。此代码适用于无 project.json:
#r "Microsoft.WindowsAzure.Storage"
open Microsoft.Azure.WebJobs;
open Microsoft.WindowsAzure.Storage.Queue;
let Run(request: string, customerId: int, userName: string, binder: IBinder) =
async {
let subscriberKey = sprintf "%i-%s" customerId userName
let attribute = new QueueAttribute(subscriberKey)
let! queue = binder.BindAsync<CloudQueue>(attribute) |> Async.AwaitTask
() //TODO: read messages from the queue
} |> Async.RunSynchronously
这将绑定到 默认存储帐户(我们在创建 Function App 时为您创建的帐户)。如果您想指向不同的存储帐户,您需要创建一个 属性数组 ,并包含一个 StorageAccountAttribute
指向您想要的存储帐户(例如 new StorageAccountAttribute("your_storage")
).然后,您将此属性数组(队列属性位于数组的第一个)传递到采用属性数组的 BindAsync
重载。有关详细信息,请参阅
但是,如果您不需要做任何复杂的事情 parsing/formatting 来形成队列名称,我认为您甚至不需要为此使用 Binder.您可以完全以声明方式绑定到队列。这是 function.json 和代码:
{
"bindings": [
{
"type": "httpTrigger",
"name": "request",
"route": "retrieve/{customerId}/{userName}",
"authLevel": "function",
"methods": [
"get"
],
"direction": "in"
},
{
"type": "queue",
"name": "queue",
"queueName": "{customerId}-{userName}",
"connection": "<your_storage>",
"direction": "in"
}
]
}
以及函数代码:
#r "Microsoft.WindowsAzure.Storage"
open Microsoft.Azure.WebJobs;
open Microsoft.WindowsAzure.Storage.Queue;
let Run(request: string, queue: CloudQueue) =
async {
() //TODO: read messages from the queue
} |> Async.RunSynchronously