依赖注入 – Azure触发的Webjobs依赖注入的范围

前端之家收集整理的这篇文章主要介绍了依赖注入 – Azure触发的Webjobs依赖注入的范围前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已阅读并询问有关如何使用WebJob以及特别是Triggered Webjobs使用DI的一些问题.

> SimpleInjector – Azure WebJob with TimerTrigger – Register IDisposable
> Dependency injection using Azure WebJobs SDK?

我仍然试图找出如何在触发的webjobs中优雅地整合DI,并且@Steven问我一个很好的问题:

Isn’t there a way to wrap the execution of your service in some scope? For instance,MVC and Web API have an IDependencyScope abstraction for this. This notifies the starting and ending of a request. To be able to do scoping,you either need to have such interception point or you need to be able to wrap the call to JobActivator.

我知道我可以在触发函数中启动一个范围但是我想知道sdk中是否有任何可扩展性点允许我们进行范围设定?

谢谢.

我已经向Azure Webjob团队打开了一个 Add IDependencyScope to handle scoping请求.

我创建了一个小型库来收集Azure Webjobs和SimpleInjector的类:

> Nuget download
> GitHub project

对于QueueTrigger和ServiceBustrigger,我来到这些解决方案:

> ServiceBusTrigger(来自这个回答:https://stackoverflow.com/a/33759649/4167200):

public sealed class ScopedMessagingProvider : MessagingProvider
{
    private readonly ServiceBusConfiguration _config;
    private readonly Container _container;

    public ScopedMessagingProvider(ServiceBusConfiguration config,Container container)
        : base(config)
    {
        _config = config;
        _container = container;
    }

    public override MessageProcessor CreateMessageProcessor(string entityPath)
    {
        return new ScopedMessageProcessor(_config.MessageOptions,_container);
    }

    private class ScopedMessageProcessor : MessageProcessor
    {
        private readonly Container _container;

        public ScopedMessageProcessor(OnMessageOptions messageOptions,Container container)
            : base(messageOptions)
        {
            _container = container;
        }

        public override Task<bool> BeginProcessingMessageAsync(BrokeredMessage message,CancellationToken cancellationToken)
        {
            _container.BeginExecutionContextScope();
            return base.BeginProcessingMessageAsync(message,cancellationToken);
        }

        public override Task CompleteProcessingMessageAsync(BrokeredMessage message,FunctionResult result,CancellationToken cancellationToken)
        {
            _container.GetCurrentExecutionContextScope()?.Dispose();
            return base.CompleteProcessingMessageAsync(message,result,cancellationToken);
        }
    }
}

您可以在JobHostConfiguration中使用自定义MessagingProvider

var serviceBusConfig = new ServiceBusConfiguration
{ 
    ConnectionString = config.ServiceBusConnectionString
};
serviceBusConfig.MessagingProvider = new ScopedMessagingProvider(serviceBusConfig,container);
jobHostConfig.UseServiceBus(serviceBusConfig);

> QueueTrigger:

public sealed class ScopedQueueProcessorFactory : IQueueProcessorFactory
{
    private readonly Container _container;

    public ScopedQueueProcessorFactory(Container container)
    {
        _container = container;
    }

    public QueueProcessor Create(QueueProcessorFactoryContext context)
    {
        return new ScopedQueueProcessor(context,_container);
    }

    private class ScopedQueueProcessor : QueueProcessor
    {
        private readonly Container _container;

        public ScopedQueueProcessor(QueueProcessorFactoryContext context,Container container)
            : base(context)
        {
            _container = container;
        }

        public override Task<bool> BeginProcessingMessageAsync(CloudQueueMessage message,cancellationToken);
        }

        public override Task CompleteProcessingMessageAsync(CloudQueueMessage message,cancellationToken);
        }
    }
}

您可以在JobHostConfiguration中使用自定义IQueueProcessorFactory,如下所示:

var config = new JobHostConfiguration();
 config.Queues.QueueProcessorFactory = new ScopedQueueProcessorFactory(container);
原文链接:https://www.f2er.com/javaschema/281991.html

猜你在找的设计模式相关文章