用于写入队列的Azure函数-我可以设置元数据吗? [英] Azure Function used to write to queue - can I set metadata?
问题描述
我可以从此页面看到,当它们用作触发器时,您可以足够简单地访问队列消息元数据属性,但是我想做相反的事情。
我有一个Azure函数将消息写入队列,但是当前它具有默认的到期时间,并且我想设置一个更短的到期时间,以便它们只在队列中生存很短的时间。
I can see from this page that you can access a queue message metadata properties simply enough when they are used as a trigger, but I want to do the opposite. I have an Azure function which writes messages to a queue, but it current has the default Expiration Time and I want to set a much shorter expiration time so they only live on the queue for a very short period.
从Azure函数将消息写入队列以设置到期时间时是否有办法?
Is there a way when writing the message to the queue from the Azure Function to set the Expiration time?
谢谢
编辑1:
一个警告是,我不知道队列的名称。这是传入消息的一部分,因此将队列名设置为输出绑定
的参数,我按照@Mikhail的建议进行了更改。现在的功能如下:
EDIT 1: One caveat is that I dont know the name of the queue ahead of time. That is part of the incoming message, so the queuename is set as a parameter of the output binding I made the change as recommended by @Mikhail. Here is the function as it stands:
#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中输出绑定:
And the output binding in my function.json:
{
"type": "CloudQueue",
"name": "$return",
"queueName": "{DeviceId}",
"connection": "myConn",
"direction": "out"
}
推荐答案
将参数类型更改为 CloudQueue
,然后添加
Change the type of your parameter to CloudQueue
, then add a message manually and set the expiration time property (or rather Time To Live).
public static void Run(string input, CloudQueue outputQueue)
{
outputQueue.AddMessage(
new CloudQueueMessage("Hello " + input),
TimeSpan.FromMinutes(5));
}
编辑:如果您的输出队列名称取决于请求,则可以使用命令式绑定:
if your output queue name depends on request, you can use imperative binding:
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));
}
这篇关于用于写入队列的Azure函数-我可以设置元数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!