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

查看:19
本文介绍了使用 .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 控制台应用程序并将其作为 Web 作业运行.最重要的是,我正在尝试为 DI 实施 Ninject.

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.

附言我正在使用 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();

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天全站免登陆