带有.NET Core和Ninject的WebJobs中没有无参数的构造函数错误 [英] No parameterless constructor error in WebJobs with .NET Core and Ninject

查看:131
本文介绍了带有.NET Core和Ninject的WebJobs中没有无参数的构造函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在此处做一些平衡.当前,Azure WebJobs不支持.NET Core.

I'm trying to do a bit of a balancing act here. Currently Azure WebJobs don't support .NET Core.

在一些帮助下,我创建了一个.NET Core控制台应用程序,并将其用作WebJob.最重要的是,我正在尝试实现Ninject for DI.

With some help, I created a .NET Core Console App and made it work as a WebJob. On top of that I'm trying to implement Ninject for DI.

代码可以很好地编译,但是当我运行它时,我得到的是没有为此对象定义无参数构造函数."错误-参见下文.

Code compiles fine but when I run it, I'm getting the "No parameterless constructor is defined for this object." error -- see below.

我可能在这里使用Azure WebJobs,.NET Core 2.0和Ninject处于一个未知的领域,但是您知道是什么原因造成的吗?

I may be in a bit of unchartered territory here with Azure WebJobs, .NET Core 2.0 and Ninject but any idea what may be causing this?

顺便说一句,我运行的代码与针对.NET Framework的WebJob完全相同.我需要迁移到.NET Core 2.0,因为我正在使用针对.NET Core 2.0的类库.我还在这些类库中使用DI,这就是为什么我尝试使用Ninject将WebJob迁移到.NET Core 2.0的原因.

BTW, I had the same exact code running as a WebJob targeting .NET Framework. I needed to migrate to .NET Core 2.0 because I'm using class libraries that target .NET Core 2.0. I'm also using DI in those class libraries which is why I'm trying to migrate my WebJob to .NET Core 2.0 using Ninject.

P.S.我正在使用Azure.WebJobs 3.0.0 beta2将.NET Core控制台应用程序转换为WebJobs.我也在使用Ninject 3.2.2

P.S. I'm using Azure.WebJobs 3.0.0 beta2 to convert my .NET Core console app to WebJobs. I'm also using Ninject 3.2.2

更新: 这是我的JobActivator代码

UPDATE: Here's my code for the JobActivator

public class BrmJobActivator : IJobActivator
    {
        private readonly IKernel _container;

        public BrmJobActivator(IKernel container)
        {
            _container = container;
        }

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

这是主程序:

class Program
    {
        static readonly IKernel Kernel = new StandardKernel();
        static JobHostConfiguration config;

        static void Main(string[] args)
        {
            Environment.SetEnvironmentVariable("AzureWebJobsDashboard", "MySettings");
            Environment.SetEnvironmentVariable("AzureWebJobsStorage", "MySettings");

            BootStrapIoc();

            config = new JobHostConfiguration();

            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            var host = new JobHost(config);
            host.RunAndBlock();
        }

        private static void BootStrapIoc()
        {
            Kernel.Load(Assembly.GetExecutingAssembly());
            config = new JobHostConfiguration
            {
                JobActivator = new BrmJobActivator(Kernel)
            };
        }
    }

更新2: 我现在收到以下错误.

UPDATE 2: I'm now getting the following error.

此错误引发在下面的行中-另请参见第二张图片. return _container.Get<T>();

This error is thrown at the following line -- also see second image. return _container.Get<T>();

更新3: 这是Functions.cs文件中的代码:

UPDATE 3: Here's the code in Functions.cs file:

public class Functions
{

   private static ISomeService1 _someService1;
   private static ISomeService2 _someService2;

   private static IConfiguration _configuration;


   public Functions(ISomeService1 someService1, ISomeService2 someService2, IConfiguration configuration)
   {
       _someService1 = someService1;
       _someService2 = someService2;
       _configuration = configuration;
    }

    public async Task ProcessQueueMessage([QueueTrigger("my-brm-queue")] QueueMessage message, TextWriter log)
    {

        // Consume service
        _someService1.DoSomething(message);

    }

}

更新4: 这是Ninject绑定类中的代码:

UPDATE 4: Here's the code in Ninject bindings class:

public class NinjectBindings : Ninject.Modules.NinjectModule
{
   IConfiguration Configuration;

   public override void Load()
   {
       // Bind to IConfiguration
       var builder = new ConfigurationBuilder();
       builder.SetBasePath(Directory.GetCurrentDirectory());
       builder.AddJsonFile("appsettings.json");
       Configuration = builder.Build();
       Bind<IConfiguration>().ToMethod(ctx => {
          return Configuration;
       });

       // Create instances of clients
       var docDbClient = new ClassLibrary1.DocumentDbClient(Configuration);
       var tsClient = new ClassLibrary2.TableStorageClient(Configuration);

       // Bind Services
       Bind<ISomeService1>().To<SomeService1>();
       Bind<ISomeService2>().To<SomeService2>();

       // Bind Repositories
       Bind<IRepository1>().To<Repository1>();
       Bind<IRepository2>().To<Repository2>();

   }
}

推荐答案

BootStrapIoc方法实例化JobHostConfiguration之后,您可以从main方法重新实例化它.

After instantiating the JobHostConfiguration from the BootStrapIoc method, you re-instantiate it from the main method.

只需在您的main方法中删除此行:

Just remove this line in your main method:

config = new JobHostConfiguration();

这篇关于带有.NET Core和Ninject的WebJobs中没有无参数的构造函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆