5

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.

Is there a way when writing the message to the queue from the Azure Function to set the Expiration time?

Thanks

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; }
}

And the output binding in my function.json:

{
"type": "CloudQueue",
"name": "$return",
"queueName": "{DeviceId}",
"connection": "myConn",
"direction": "out"
}
LDJ
  • 6,896
  • 9
  • 52
  • 87

1 Answers1

7

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));
}

Edit: 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));
}
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • Thanks for the response. Seems easy enough but 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. Does that matter? See my edit for where I'm at now – LDJ Jun 10 '17 at 17:11
  • @LDJ Extended my answer to cover this. – Mikhail Shilkov Jun 10 '17 at 17:31
  • Worked an absolute charm. I'll have to read up on IBinder - thanks for the help :) – LDJ Jun 10 '17 at 17:40
  • OK, I noticed an oddity here. Its not using the connection field of my function.json for the output queue connection. I can change it to any value (or leave it blank) and it still outputs to a fixed (different) storage account to before. Where do I configure this? – LDJ Jun 12 '17 at 13:17
  • @LDJ Make a separate question for this, I would say – Mikhail Shilkov Jun 12 '17 at 13:36