Azure Function used to write to the queue - can I set the metadata?
I can see from this page that you can access the metadata properties of a queue message quite simply when used as a trigger, but I want to do the opposite. I have an Azure function that writes messages to a queue, but this current has a default expiration time and I want to set a much shorter expiration time, so they will only live on the queue for a very short period of time.
Is there a way to write a message to the queue from Azure Function to set the expiration time?
thank
EDIT 1: One caveat is that I don't know the queue name ahead of time. This is part of the inbound message, so queuename is set as the output binding parameter I made the change as @Mikhail recommended. This is how it works:
#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; }
}
And binding the output in my .json function:
{
"type": "CloudQueue",
"name": "$return",
"queueName": "{DeviceId}",
"connection": "myConn",
"direction": "out"
}
source to share
Change the type of your parameter to CloudQueue
, then add the message manually and set the expiration (or rather, time to live) property.
public static void Run(string input, CloudQueue outputQueue)
{
outputQueue.AddMessage(
new CloudQueueMessage("Hello " + input),
TimeSpan.FromMinutes(5));
}
Edit: if your output queue name depends on the request, you can use a binding 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));
}
source to share