2017-08-30 54 views
0

我试图将我的代码从运行在.NET Framework 4.6.1上的Webjobs项目迁移到新的.NET Core 2.0控制台项目。我得到错误的一些错误的位置:在.NET Core控制台中使用Ninject应用

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代码中的错误:

public class BrmJobActivator : IJobActivator 
{ 
    private readonly IKernel _container; 

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

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

UPDATE: 这是下的NuGet包在我的项目在安装后的警告信息Ninject包3.2.2: enter image description here

回答

1

另外这里得到一个错误,上面写着:参数1:无法转换System.Reflection.Assembly到System.Co llections.Generic.IEnumerable

Ninject的最新预发布版本有一些变化。请改为安装最新的稳定版本3.2.2。

enter image description here

我测试了我的身边你的代码。将Ninject版本更新到3.2.2后,代码运行良好。

+0

谢谢你的回应。我安装了版本3.2.2,它似乎工作正常。不过,我在NuGet包中看到一条警告。我拍了一个屏幕截图,并用它更新了原始文章。该消息警告说这个软件包可能与我的项目不完全兼容。 – Sam

+0

只是为了进一步阐明:项目的构建没有错误,我不知道以前遇到的错误。 – Sam

+0

您可以忽略它或修改代码以使用最新版本的NInject。你需要实现一个Ninject.Modules.NinjectModule。以下链接供您参考。 https://github.com/ninject/Ninject/wiki/Modules-and-the-Kernel – Amor

0

Ninject 3.3.0于2017年9月26日发布,现在的目标是.NET标准2.0,因此也运行在.NET Core 2.0上。更新到3.3.0将修复警告。

相关问题