C# Azure WebJobs 3.0上的IQueueProcessorFactory?
使用Azure WebJobs 2.0来实现C# Azure WebJobs 3.0上的IQueueProcessorFactory?,c#,azure,azure-webjobs,C#,Azure,Azure Webjobs,使用Azure WebJobs 2.0来实现IQueueProcessorFactory的实例,我必须执行以下操作: _jobHostConfiguration = new JobHostConfiguration { StorageConnectionString = "XXX" DashboardConnectionString = "XXX" }; _jobHostConfiguration.Queues.Qu
IQueueProcessorFactory
的实例,我必须执行以下操作:
_jobHostConfiguration = new JobHostConfiguration {
StorageConnectionString = "XXX"
DashboardConnectionString = "XXX"
};
_jobHostConfiguration.Queues.QueueProcessorFactory = new 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);
}
}
其中CustomQueueProcessorFactory
类似于:
_jobHostConfiguration = new JobHostConfiguration {
StorageConnectionString = "XXX"
DashboardConnectionString = "XXX"
};
_jobHostConfiguration.Queues.QueueProcessorFactory = new 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做同样的事情
我找不到任何示例。在Webjob 3.0中,
AddAzureStorage
中的属性QueuesOptions
没有QueueProcessorFactory
因此,您可以使用builder.ConfigureServices(s=>s.AddSingleton(factory))代码>注入iqueProcessorFactory
。请参考以下步骤:
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);
}
}