Search code examples
c#.netninject.net-standard

.NET Standard + .NET Framework: could not load file or assembly 'Ninject'


SITUATION

I have the following solution structure:

 - solution
     - "WPF Client" PROJECT[.NET Framework 4.6.1] 
       (has ref to: "Core Logic" PROJECT, "Service Provider" PROJECT)
    
     - "Core Logic" PROJECT[.NET Framework 4.6.1] 
       (has ref to: "Service Provider" PROJECT)
    
     - "Service Provider" PROJECT[.NET Standard 2.0] 
       (has nuget: "Ninject")

and I have following classes

IoC.cs in Service Provider PROJECT

public static class IoC
{
    private static readonly IKernel mKernel;

    static IoC()
    {
        mKernel = new StandardKernel();

        Setup();
    }

    private static void Setup()
    {
        mKernel.Bind<IInput>().To<Input>();
    }

    public static T Get<T>()
    {
        return mKernel.Get<T>();
    }
}

MyViewModel.cs in Core Logic PROJECT

public sealed class MyViewModel
{
    ...
    public void MyViewModelMethod()
    {
        var a = IoC.Get<IInput>(); 
    }
    ...
}

MyView.xaml.cs in WPF Client PROJECT

public partial class MyView : UserControl
{
    public MyView()
    {
        InitializeComponent();
    
        var viewModel = new MyViewModel();
        viewModel.MyViewModelMethod();
    }

}

PROBLEM

whenever I try to run my project it throws following error:

could not load file or assembly 'Ninject'

Edit

I think this does NOT have to do anything with ninject... it's rather a .net standard + .net 4.6.1 issue.

the reason I'm thinking this way is: first of I tried other IoC containers and they threw the same error message. BUT it doesn't end there... whenever I install any NuGet package on my .NET Standard Project it seems to not be able to load its assemblies into my client project (.net 4.6.1)

the question is: is there any workaround to this problem?


Solution

  • Found the solution!

    You have to migrate Your packages.config into Your .csproj file.

    for more details lookup here.