c# - 多个 azure 队列的一个监听器

标签 c# azure message-queue azure-webjobs

我想创建一个 Web 作业(监听器)来监听存储中的所有队列。如果有任何新消息,则会触发处理程序。

Azure WebJobs SDK 提供了一种仅监听一个队列的解决方案:

public class Functions
{

    // This function will get triggered/executed when a new message is written 
    // on an Azure Queue called queue.
    public static async Task ProcessQueueMessage(
        [QueueTrigger("%test%")] CloudQueueMessage message,
        IBinder binder)
    {
        //do some stuff
    }
}

这种方法很好,但我需要: 1)监听不同的队列 2)向这个类注入(inject)一个我认为我不能的类

所以我正在考虑创建自己的监听器。我想创建多个威胁,每个威胁都会监听一个队列。然后,当我运行网络作业时,它会启动所有威胁。

我想知道是否有人可以提出更好的解决方案。如果有代码示例就太好了。

谢谢

最佳答案

除非您确实想要,否则不需要创建自己的监听器。 Azure Webjobs SDK 已经为您完成了繁重的工作。

这是一个示例 Functions.cs,它可以处理来自不同队列的数据。 如果需要,您可以将服务注入(inject)到 Functions.cs 中,以便不同的队列由不同的服务处理。

    private readonly IMyService _myService;

    //You can use Dependency Injection if you want to. 
    public Functions(IMyService myService)
    {
        _myService = myService;
    }

    public void ProcessQueue1([QueueTrigger("queue1")] string item)
    {
        //You can get the message as a string or it can be strongly typed
        _myService.ProcessQueueItem1(item);
    }

    public void ProcessQueue2([QueueTrigger("queue2")] MyObject item)
    {
        //You can get the message as a string or it can be strongly typed
        _myService.ProcessQueueItem2(item);
    }

希望这有帮助

关于c# - 多个 azure 队列的一个监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46964689/

相关文章:

c# - 使用 Dataview.RowFilter 从 SomeTable 中选择 TOP 5 *?

asp.net-mvc - Azure Blob 文件存储策略

c# - Azure 服务总线 SubscriptionClient 高延迟/无法同时接收消息

rabbitmq - 是否应该在 RabbitMQ 中多次声明交换或队列?

c - 使用 IPC 队列发送消息时,标识符已删除 (EIDRM) 错误

c# - SpecFlow 可重用步骤定义

c# - Convert.ToInt32 用户输入数组转换为 int c#

html - 带 tomcat 的 Azure Windows 服务器不显示完整的 html 页面

wcf - 4.0/WCF : Best approach for bi-idirectional message bus?

c# - Json.Net 与 Asp.Net Mvc 中的 JsonRequestBehavior 等价物