【问题标题】:IQueueProcessorFactory on Azure WebJobs 3.0?Azure WebJobs 3.0 上的 IQueueProcessorFactory?
【发布时间】:2019-08-29 14:42:55
【问题描述】:

使用 Azure WebJobs 2.0 来实现 IQueueProcessorFactory 的实例,我必须这样做:

_jobHostConfiguration = new JobHostConfiguration {
        StorageConnectionString = "XXX"
        DashboardConnectionString = "XXX"                
        };
_jobHostConfiguration.Queues.QueueProcessorFactory = new CustomQueueProcessorFactory();

CustomQueueProcessorFactory 是这样的:

public class CustomQueueProcessorFactory: IQueueProcessorFactory
{
    public QueueProcessor Create(QueueProcessorFactoryContext context)
    {
        if (context == null) throw new ArgumentNullException(nameof(context));

        if (context.Queue.Name.Equals("queue_A") {
            context.BatchSize = 32; 
            context.NewBatchThreshold = 100;
        } 

        if (context.Queue.Name.Equals("queue_B")) {
            context.BatchSize = 2; 
        } 

        return new QueueProcessor(context);
    }        
}

我的问题是:如何使用 Azure WebJobs 3.0 做同样的事情?

我找不到任何样本。

【问题讨论】:

    标签: c# azure azure-webjobs


    【解决方案1】:

    在 Webjob 3.0 中,AddAzureStorage 中的属性 QueuesOptions 没有 QueueProcessorFactory。

    因此,您可以使用builder.ConfigureServices(s => s.AddSingleton<IQueueProcessorFactory>(factory)); 注入IQueueProcessorFactory。请参考以下步骤:

    1.Program.cs.

    class Program
    {
        static void Main(string[] args)
        {
            var builder = new HostBuilder();
            var factory = new CustomQueueProcessorFactory();
            builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                b.AddAzureStorage();
            });
            builder.ConfigureServices(s => s.AddSingleton<IQueueProcessorFactory>(factory));
            builder.ConfigureAppConfiguration((context, config) =>
            {
                config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            });
            var host = builder.Build();
            using (host)
            {
                host.Run();
            }
        }
    }
    
    public class CustomQueueProcessorFactory : IQueueProcessorFactory
    {
        public QueueProcessor Create(QueueProcessorFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (context.Queue.Name.ToString() == "queue")
            {
                context.MaxDequeueCount = 10;
            }
            else if (context.Queue.Name.ToString() == "queue1")
            {
                context.MaxDequeueCount = 10;
                context.BatchSize = 1;
            }
            return new QueueProcessor(context);
        }
    }
    

    2.Function.cs:

    public static void ProcessQueueMessage([QueueTrigger("queue1")] string message, TextWriter log)
    {
        log.WriteLine(message);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多