Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure WebJobs 3.0上的IQueueProcessorFactory?_C#_Azure_Azure Webjobs - Fatal编程技术网

C# Azure WebJobs 3.0上的IQueueProcessorFactory?

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

使用Azure WebJobs 2.0来实现
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);
    }
}