首页 > 解决方案 > 具有变量名称、QueueTriggerAttribute 限制的 Azure Functions QueueTrigger

问题描述

假设以下典型的队列触发函数:

public void Run([QueueTrigger("queue1")]object data, ILogger log)
{
    // Do something with data
}

我的问题是它"queue1"必须是一个常量字段,所以它必须在编译时定义。另外,我想要一个队列触发器的基类,它可以像这样工作:

public abstract class QueueBase<TModel>
{
    public void Run([QueueTrigger("queueName")]TModel data, ILogger log)
    {
        // Do something with data, log something etc.
        OnRunExecuted(data);
        // Do something with data, log something etc.
    }

    public abstract void OnRunExecuted(TModel data);
}

有了这个,我可以编写自己的类,这些类继承自QueueBase但甚至可以存在于没有Microsoft.Azure.WebJobs依赖关系的库中:

public class MyQueueHandler : QueueBase<MyModel>
{
    public void OnRunExecuted(MyModel data) => ...;
}

但是不可能传入队列名称......是吗?

标签: c#.net.net-coreazure-functions

解决方案


我记得属性QueueTrigger只接受 const 字符串,所以你可以尝试使用环境变量来做一些技巧,比如 post how to pass dynamic queue name


推荐阅读