在.NET Core控制台应用程序中使用Ninject [英] Using Ninject in .NET Core Console App

查看:538
本文介绍了在.NET Core控制台应用程序中使用Ninject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的代码从在.NET Framework 4.6.1上运行的Webjobs项目迁移到新的.NET Core 2.0 Console项目.我在这里遇到一些错误:

I'm trying to migrate my code from a Webjobs project runing on .NET Framework 4.6.1 to a new .NET Core 2.0 Console project. I'm getting errors some errors here:

class Program
{
   // Here I'm getting IKernel is obsolete. Use IKernelConfiguration and IReadOnlyKernel message.
   // Also a message that reads: StandardKerynel is obsolete. Use StandardKernelConfiguration and StandardReadOnlyKernel 
   static readonly IKernel Kernel = new StandardKernel();
   static JobHostConfiguration config;

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

      BootStrapIoc();

      config = new JobHostConfiguration();

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

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

   private static void BootStrapIoc()
   {
      // Also getting an error here that reads: Argument 1: Cannot convert System.Reflection.Assembly to System.Collections.Generic.IEnumerable<Ninject.Modules.NinjectModule>
      Kernel.Load(Assembly.GetExecutingAssembly());
      config = new JobHostConfiguration
      {
         JobActivator = new BrmJobActivator(Kernel)
      };
   }
}

我的BrmJobActivator代码中也出现错误:

I'm also getting errors in my BrmJobActivator code:

public class BrmJobActivator : IJobActivator
{
   private readonly IKernel _container;

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

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

更新: 这是安装Ninject软件包3.2.2后我项目中NuGet软件包下的警告消息:

UPDATE: This is the warning message under NuGet packages in my project after installing Ninject package 3.2.2:

推荐答案

在这里也显示错误:参数1:无法将System.Reflection.Assembly转换为System.Collections.Generic.IEnumerable

Also getting an error here that reads: Argument 1: Cannot convert System.Reflection.Assembly to System.Collections.Generic.IEnumerable

Ninject的最新预发行版本中有一些更改.请安装最新的稳定版3.2.2版本.

There are some changes in the latest prerelease version of Ninject. Please install the latest stable 3.2.2 version instead.

我站在一边测试了您的代码.将Ninject版本更新到3.2.2后,代码可以正常工作.

I tested your code on my side. After updated the Ninject version to 3.2.2, the code worked fine.

这篇关于在.NET Core控制台应用程序中使用Ninject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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