IServiceProvider与Autofac和webjob

时间:2018-01-10 19:28:04

标签: dependency-injection autofac azure-webjobs

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

当我使用Microsoft.Extensions.DependencyInjection.ServiceCollection()注册我的界面代替 Autofac 时,它正在运行(我不知道IServiceProvider的注册位置但它正在运作)。

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

有人有想法吗?

2 个答案:

答案 0 :(得分:3)

  

我需要调用使用IServiceProvider的代码,此时我从Autofac收到错误,因为IServiceProvider未知。   这是否意味着在运行Webjob时,您无法在AutofacActivator中找到IServiceProvider。   我不清楚你如何定义IServiceProvider以及如何注入它?

我认为您可以将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));
        }
    }

在Progtam:

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>();
        }
}

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

答案 1 :(得分:0)

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

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

使用它们执行以下操作:

IServiceProvider

它会自动为您生成//get template var SStemplate = "URL here"; var SStocopy = SpreadsheetApp.openByUrl(SStemplate); //set destination folder var SSdestFolder = DriveApp.getFolderById("0B-hjh1DycmoweEl5ZmgxdFBRYzg"); //copy template to destination var newFile = DriveApp.getFileById(SStocopy.getId()).makeCopy(name, SSdestFolder); //open the new file var ss = SpreadsheetApp.openById(newFile.getId()); var sheet = ss.getActiveSheet(); //Write the value you want in C3 cell sheet.getRange("C3").setValue("test");