我可以从this page看到当队列消息元数据属性用作触发器时,您可以简单地访问它们,但我想做相反的事情。 我有一个将消息写入队列的 Azure 函数,但它当前具有默认的过期时间,我想设置更短的过期时间,以便它们仅在队列中存活很短的时间。
从Azure函数将消息写入队列时有没有办法设置过期时间?
谢谢
编辑1: 需要注意的是,我提前不知道队列的名称。这是传入消息的一部分,因此队列名称被设置为输出绑定(bind)的参数 我按照@Mikhail 的建议进行了更改。这是目前的功能:
#r "Microsoft.WindowsAzure.Storage"
#r "Newtonsoft.Json"
using System;
using Microsoft.WindowsAzure.Storage.Queue;
using Newtonsoft.Json;
public static void Run(MyType myEventHubMessage, CloudQueue outputQueue, TraceWriter log)
{
var deviceId = myEventHubMessage.DeviceId;
var data = JsonConvert.SerializeObject(myEventHubMessage);
var msg = new CloudQueueMessage(data);
log.Info($"C# Event Hub trigger function processed a message: {deviceId}");
outputQueue.AddMessage(msg, TimeSpan.FromMinutes(3), 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 中的输出绑定(bind):
{
"type": "CloudQueue",
"name": "$return",
"queueName": "{DeviceId}",
"connection": "myConn",
"direction": "out"
}
最佳答案
将参数类型更改为 CloudQueue
,然后手动添加消息并设置过期时间属性(或更确切地说生存时间)。
public static void Run(string input, CloudQueue outputQueue)
{
outputQueue.AddMessage(
new CloudQueueMessage("Hello " + input),
TimeSpan.FromMinutes(5));
}
编辑:如果您的输出队列名称取决于请求,您可以使用命令式绑定(bind):
public static void Run(string input, IBinder binder)
{
string outputQueueName = "outputqueue " + input;
QueueAttribute queueAttribute = new QueueAttribute(outputQueueName);
CloudQueue outputQueue = binder.Bind<CloudQueue>(queueAttribute);
outputQueue.AddMessage(
new CloudQueueMessage("Hello " + input),
TimeSpan.FromMinutes(5));
}
关于c# - Azure Function 用于写入队列 - 我可以设置元数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44472725/