【问题标题】:one listener for multiple azure queues多个 Azure 队列的一个侦听器
【发布时间】:2018-04-08 10:45:22
【问题描述】:

我想创建一个 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)向这个类注入一个我认为我不能的类

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

我想知道是否有人可以提出更好的解决方案。拥有代码示例真的很棒。

谢谢

【问题讨论】:

  • 您可以从 Funcions.cs 中监听任意数量的队列,只需将不同的队列名称放在不同的触发器中即可。
  • @lopezbertoni,你应该把它写成答案。

标签: c# azure message-queue azure-webjobs


【解决方案1】:

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

这是一个可以处理来自不同队列的数据的示例 Functions.cs。 您可以将服务注入 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);
    }

希望对你有帮助

【讨论】:

  • 感谢您的帖子。您的解决方案部分有效。是的,我现在可以收听不同的队列,这很棒。但是,当我运行应用程序时,构造函数没有运行。事实上,我得到了这个错误:Microsoft.Azure.WebJobs.Host.FunctionInvocationException:执行函数时出现异常:Functions.ProcessQueueMessage1 ---> System.MissingMethodException:没有为此对象定义无参数构造函数。当我添加空构造函数时,永远不会调用另一个构造函数。有什么想法吗?
【解决方案2】:

正如@lopezbertoni 建议的那样,我在函数中创建了两个方法,并且我使用 IJobActivator 将类注入到函数中。请参见下面的示例:

public class Program
{
    static void Main()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<MyMessageHandler>().As<IMessageHandler>();
        builder.RegisterType<Functions>()
            .InstancePerDependency();


        var host = new JobHost(new JobHostConfiguration
        {
            JobActivator = new AutofacJobActivator(builder.Build())
        });

        host.RunAndBlock();
    }
}

public class AutofacJobActivator : IJobActivator
{
    private readonly IContainer _container;

    public AutofacJobActivator(IContainer container)
    {
        _container = container;
    }

    public T CreateInstance<T>()
    {
        return _container.Resolve<T>();
    }
}

public class Functions
{
    private IMessageHandler _myService;

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

    // This function will get triggered/executed when a new message is written 
    // on an Azure Queue called queue.
    public async Task ProcessQueueMessage1(
        [QueueTrigger("test1")] CloudQueueMessage message,
        IBinder binder)
    {
        _myService.HandleMessage(message.AsString);
        Console.WriteLine("ProcessQueueMessage1 was run");
        await Task.CompletedTask;
    }

    public async Task ProcessQueueMessage2(
        [QueueTrigger("test2")] CloudQueueMessage message,
        IBinder binder)
    {
        _myService.HandleMessage(message.AsString);
        Console.WriteLine("ProcessQueueMessage2 was run");
        await Task.CompletedTask;
    }
}

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 2013-09-24
    • 2021-02-18
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 2017-10-29
    • 2013-06-16
    • 1970-01-01
    相关资源
    最近更新 更多