c# - Azure Function 用于写入队列 - 我可以设置元数据吗?

标签 c# azure azure-functions azure-queues

我可以从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/

相关文章:

Azure Functions : Can a shared . csx 使用 nuget 包?

c# - 如何反向遍历 C# 集合?

c# - 如何以编程方式将 CamelCase 名称更改为可显示的名称

适用于 Linux 的 Azure 扩展脚本

azure - AADSTS9002325 : Proof Key for Code Exchange is required for cross-origin authorization code redemption

powershell - Key Vault 返回 401 和访问 token (MSI PowerShell Function App)

.net - 找不到 microsoft.extensions.logging.abstractions

c# - MemoryStream - 合并 PDF 时无法访问关闭的流

c# - 如何格式化带有千位分隔符和小数点分隔符的 Windows 窗体文本框以进行数字输入

sql-server - 无法使用 PowerShell 或 T-SQL 将 SQL 数据库备份到 Azure Blob 存储