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

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

问题描述

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

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