1

My scenario for an Azure function:

  1. HTTP trigger.
  2. Based on HTTP parameters I want to read messages from an appropriate storage queue and return data back.

Here is the code of the function (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

The compilation succeeds (with proper NuGet references and opening packages), but I get the runtime exception:

Microsoft.Azure.WebJobs.Host: 
Can't bind Queue to type 'Microsoft.WindowsAzure.Storage.Queue.CloudQueue'.

My code is based on an example from this article.

What am I doing wrong?

Update: now I realize I haven't specified Connection Name anywhere. Do I need a binding for the IBinder-based queue access?

Update 2: my function.json file:

{
  "bindings": [
    {
      "type": "httpTrigger",
      "name": "request",
      "route": "retrieve/{customerId}/{userName}",
      "authLevel": "function",
      "methods": [
        "get"
      ],
      "direction": "in"
   }
 ],
 "disabled": false
}
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107

1 Answers1

3

I suspect that you're having versioning issues because you're bringing in a conflicting version of the Storage SDK. Instead, use the built in one (w/o bringing in any nuget packages). This code works with no 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

This will bind to the default storage account (the one we created for you when your Function App was created). If you want to point to a different storage account, you'll need to create an array of attributes, and include a StorageAccountAttribute that points to your desired storage account (e.g. new StorageAccountAttribute("your_storage")). You then pass this array of attributes (with the queue attribute first in the array) into the BindAsync overload that takes an attribute array. See here for more details.

However, if you don't need to do any sophisticated parsing/formatting to form the queue name, I don't think you even need to use Binder for this. You can bind to the queue completely declaratively. Here's the function.json and code:

{
  "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"
    }
  ]
}

And the function code:

#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
mathewc
  • 13,312
  • 2
  • 45
  • 53
  • When I tried removing NuGet reference I got `Could not load file or assembly 'Microsoft.WindowsAzure.Storage, Version=8.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).` – Mikhail Shilkov Feb 17 '17 at 17:23
  • Based on https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue I thought input bindings are not supported for queues... Thanks, I'll try that! – Mikhail Shilkov Feb 17 '17 at 17:32
  • I tried copy-pasting your 2nd code, but Portal gives me errors about `Microsoft.WindowsAzure.Storage` not found and then `We are unable to reach your function app (Not Found). Please try again later`. – Mikhail Shilkov Feb 17 '17 at 18:04
  • Are you on our latest ~1 Functions version? That code works for me in production on our latest bits. Try creating a new function and using my exact code + metadata. – mathewc Feb 17 '17 at 19:44
  • 1
    Ok, got it working by copy-pasting... Not sure what the problem was. – Mikhail Shilkov Feb 17 '17 at 20:51