带有 Autofac 和 webjob 的 IServiceProvider

IServiceProvider with Autofac and webjob

我在 Azure webJob 中工作。 我正在使用 autofacIJobActivator,一切正常。 但是现在,我需要调用使用 IServiceProvider 的代码,那时我从 Autofac 收到错误,因为 IServiceProvider 未知。

当我使用 Microsoft.Extensions.DependencyInjection.ServiceCollection() 代替 Autofac 注册我的接口时,它正在工作(我不知道在哪里IServiceProvider 已注册但正在运行)。

我工作的公司要求我明确使用 Autofac。 我很难找到使用 Autofac 的方法,但在 WebJob.

中声明了 IServiceProvider

有人有想法吗?

I need to call code that is using IServiceProvider and at that moment I get an error from Autofac because IServiceProvider is not known. Does that mean when running your Webjob, you could not find the IServiceProvider in AutofacActivator. I am not clear about how do you define IServiceProvider and how do you inject it?

我想你可以将 IServiceProvider 注入到作业激活器中并注册它,然后你就可以使用这个实例来获得服务。

您可以在构建之前在 ContainerConfig 中注册 IServiceProvider 接口:

public static class ContainerConfig
    {
        public static IContainer GetContainer()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<Functions>();

            builder.RegisterType<HelloGenerator>().As<IStringGenerator>().SingleInstance();
            builder.Register<IServiceProvider>(context =>
        {
            var serviceCollection = new ServiceCollection();
            //todo: register the interfaces
            return serviceCollection.BuildServiceProvider();
        }).SingleInstance();

            return builder.Build();
        }
}

在函数中触发时获取服务:

public class Functions
    {
        private readonly IStringGenerator _stringGenerator;
        private readonly IServiceProvider _serviceProvider;
        public Functions(IStringGenerator strGenerator,IServiceProvider serviceProvider)
        {
           _stringGenerator = strGenerator;
            _serviceProvider = serviceProvider;
        }
        public void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
        {
           log.WriteLine(_stringGenerator.GetWord());
            log.WriteLine(_serviceProvider.GetService(xxxxxx));
        }
    }

在程序中:

static void Main()
        {
            var config = new JobHostConfiguration
            {
                JobActivator = new AutofacActivator(ContainerConfig.GetContainer())
            };
            var host = new JobHost(config);

            host.RunAndBlock();
        }

在 AutofacActivator 中:

public class AutofacActivator : IJobActivator
    {
        private readonly IContainer _container;

        public AutofacActivator(IContainer container)
        {
            _container = container;
        }

        public T CreateInstance<T>()
        {
            return _container.Resolve<T>();
        }
}

如果这不是你想要的,希望你能给我更详细的描述和你的主要思想代码。

事实上,我通过使用扩展解决了我的问题:

using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

通过使用它们执行以下操作:

_containerBuilder = new ContainerBuilder();
_containerBuilder.Populate(new ServiceCollection());
_containerBuilder.RegisterType<MyGreatType>().InstancePerDependency();
_container = _containerBuilder.Build();

它会自动为您生成IServiceProvider