从使用类型 CloudQueue 的 Azure 函数中指定存储帐户的名称
Specify the name of storage account from Azure Function that uses type CloudQueue
我有一个 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 中完全删除值或 key/value 对,该函数仍然运行并写入某个地方似乎是默认存储帐户的内容。
我已经尝试将 [StorageAccount("NAME_OF_CON_STR_I_WANT_TO_USE")]
属性添加到我的 运行 函数中,但这似乎也被忽略了,我还尝试创建一个属性数组并传递 QueueAttribute 和 StorageAccountAttribute
到 binder.Bind<T>(attributeArray)
但抱怨它不能接受数组。
有谁知道它从哪里获取它正在使用的存储帐户,更重要的是我如何设置存储帐户名称?
谢谢
您走在正确的轨道上:您需要将属性数组中的 StorageAccountAttribute
传递给活页夹。由于我不知道的原因,看起来只有异步版本的具体 class Binder
方法支持传递数组。类似的东西应该可以工作:
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);
// ...
}
顺便说一下,您不需要在 function.json
中为命令式绑定指定任何配置:它们将被忽略。只需删除那些以避免混淆(当然保留触发器)。
我有一个 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 中完全删除值或 key/value 对,该函数仍然运行并写入某个地方似乎是默认存储帐户的内容。
我已经尝试将 [StorageAccount("NAME_OF_CON_STR_I_WANT_TO_USE")]
属性添加到我的 运行 函数中,但这似乎也被忽略了,我还尝试创建一个属性数组并传递 QueueAttribute 和 StorageAccountAttribute
到 binder.Bind<T>(attributeArray)
但抱怨它不能接受数组。
有谁知道它从哪里获取它正在使用的存储帐户,更重要的是我如何设置存储帐户名称?
谢谢
您走在正确的轨道上:您需要将属性数组中的 StorageAccountAttribute
传递给活页夹。由于我不知道的原因,看起来只有异步版本的具体 class Binder
方法支持传递数组。类似的东西应该可以工作:
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);
// ...
}
顺便说一下,您不需要在 function.json
中为命令式绑定指定任何配置:它们将被忽略。只需删除那些以避免混淆(当然保留触发器)。