gpt4 book ai didi

c# - Azure WebJobs 3.0 上的 IQueueProcessorFactory?

转载 作者:行者123 更新时间:2023-12-02 23:17:50 26 4
gpt4 key购买 nike

使用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做同样的事情?

我找不到任何样本。

最佳答案

在 Webjob 3.0 中,属性 QueuesOptionsAddAzureStorage没有 QueueProcessorFactory。

所以,你可以使用builder.ConfigureServices(s => s.AddSingleton<IQueueProcessorFactory>(factory));注入(inject)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);
}

关于c# - Azure WebJobs 3.0 上的 IQueueProcessorFactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com