从Azure Function中指定使用CloudQueue类型的存储帐户的名称

时间:2017-06-12 15:00:08

标签: c# azure azure-functions

我有一个Azure函数,它接收来自IoTHub的消息并将其放在给定队列上进行处理。队列在运行时由传入的消息数据动态确定,并以短的ExpirationTime放置在队列中,因为我只希望消息持续几秒钟:

#r "Microsoft.WindowsAzure.Storage"
#r "Newtonsoft.Json"

using System;
using Microsoft.WindowsAzure.Storage.Queue;
using Newtonsoft.Json;


public static void Run(MyType myEventHubMessage, IBinder binder, TraceWriter log)
{
    int TTL = 3;
    var data = JsonConvert.SerializeObject(myEventHubMessage);
    var msg = new CloudQueueMessage(data);

    string outputQueueName = myEventHubMessage.DeviceId;
    QueueAttribute queueAttribute = new QueueAttribute(outputQueueName);
    CloudQueue outputQueue = binder.Bind<CloudQueue>(queueAttribute);

    outputQueue.AddMessage(msg, TimeSpan.FromSeconds(TTL), null, null, null);

}

public class MyType
{
  public string DeviceId { get; set; }
  public double Field1 { get; set; }
  public double Field2 { get; set; }
  public double Field3 { get; set; }

}

这个工作正常,消息正在写入我的队列。但是,它被写入的队列不是我想要使用的存储帐户!它似乎从其他地方购买了一个存储帐户?!?

我的function.json中有一个连接属性:

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "myEventHubMessage",
      "direction": "in",
      "path": "someiothub",
      "connection": "IoTHubConnectionString"
    },
    {
      "type": "CloudQueue",
      "name": "$return",
      "queueName": "{DeviceId}",
      "connection": "NAME_OF_CON_STRING_I_WANT_TO_USE",
      "direction": "out"
    }
  ],
  "disabled": false
}

但完全被忽略了。从表面上看,我可以从JSON中完全删除值或键/值对,并且该函数仍然运行并写入一些看似默认的存储帐户。

我尝试将[StorageAccount("NAME_OF_CON_STR_I_WANT_TO_USE")]属性添加到我的Run函数中,但似乎也被忽略了,我还尝试创建一个Attribute数组并将QueueAttribute和StorageAccountAttribute传递给{{1}但是抱怨它无法接受数组。

有没有人首先知道从哪个位置获取存储帐户,更重要的是我如何设置存储帐户名称?

由于

1 个答案:

答案 0 :(得分:1)

You are on the right track: you need to pass StorageAccountAttribute in an array of attributes to the binder. To a reason which is unknown to me, it looks like only async version of concrete class Binder method supports passing an array. Something like that should work:

public static async Task Run(MyType myEventHubMessage, Binder binder, TraceWriter log)
{
    // ...
    var queueAttribute = new QueueAttribute(outputQueueName);
    var storageAttribute = new StorageAccountAttribute("MyAccount");
    var attributes = new Attribute[] { queueAttribute, storageAttribute };
    CloudQueue outputQueue = await binder.BindAsync<CloudQueue>(attributes);
    // ...
}

By the way, you don't need to specify any configuration in function.json for imperative bindings: they will be ignored. Just delete those to avoid confusion (keep the trigger, of course).