微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

依赖注入 – 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.jb51.cc/javaschema/281991.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐