20

I am trying to install Ninject 3.3.2 in .NET Core, Released in May 2016. I got an error: The dependency Ninject 3.2.2 does not support framework .NETCoreApp, Version=v1.0. Does anybody had similar problem, and is there any solution for this?

svick
  • 236,525
  • 50
  • 385
  • 514
Dejan Dimčić
  • 294
  • 1
  • 3
  • 9

4 Answers4

36

Ninject 3.3.0 was released September 26th 2017 and now targets .NET Standard 2.0 and thus also runs on .NET Core 2.0.

From the course of things (see issues/discussions on GitHub) it seems likely that some of the changes in the 4.0-beta will be reverted. I would not expected a 4.0 final shortly. Hence I would advise to go with the current version 3 release.

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
  • Any documentation or pointers on how to integrate this into an ASP.Net Core project, assuming an AspNetCore Ninject extension isn't currently available? – SimonGates Oct 05 '17 at 09:41
  • You can implement needed interface by yourself: http://kristian.hellang.com/third-party-dependency-injection-in-asp-net-core/ – VMAtm Oct 07 '17 at 00:20
  • Note that Xamarin.iOS does not support the System.Reflection.Emit namespace. This meant using Ninject built for .NET Standard was impossible for me. I had to use the library built for MonoTouch. – Joseph Feb 15 '18 at 23:36
16

Just wanted to add; while both of the previous answers are correct in that ASP.Net core does provide built in dependency injection, it is NOT sufficient for more advanced scenarios. As it does not support a whole host of features that Ninject, AutoFac, Unity, or StructureMap supports.

At present, the only DI libraries that I am aware of that fully supports .net core are AutoFac and now Unity as well. It is very simple to add this in. The only thing you need to do to replace the built in DI is as follows. This example is for AutoFac but its almost identical for Unity it looks like.

First, replace the void on ConfigureServices in startup.cs with an IServiceProvider (dependency from AutoFac) like so:

public IServiceProvider ConfigureServices(IServiceCollection services)

Then create a container builder, build and resolve an IServiceProvider from ConfigureServices:

var builder = new ContainerBuilder();
builder.Populate(services);
var container = builder.Build();
return container.Resolve<IServiceProvider>();

I have a wrapper around the this second part that allows you to dynamically load and build different configurations using AutoFac modules, that I might be convinced to upload to GitHub or something if there is any interest.

Community
  • 1
  • 1
Brandon
  • 830
  • 1
  • 15
  • 35
  • 4
    I should also add, that I ran across the github repo that has a sort of "adapter" that allows you to use Ninject in .net core. Its a bit of a 'workaround' in my opinion, but seems like should work. https://github.com/dotnetjunkie/Missing-Core-DI-Extensions – Brandon Nov 14 '16 at 16:21
  • Looks like Ninject 4.0 might be supporting .net core. Although its still in beta. – Brandon Jan 03 '17 at 19:16
5

Ninject does not support .NET Core. You can check it's website to be sure if there is no version that supports it.

ASP.NET Core has its own Dependency Injection container build in. See here.

4444
  • 3,541
  • 10
  • 32
  • 43
  • 5
    That bundled IOC/DI is really simple one. You should use proper container like Autofac or StructureMap and just ignore built in container. – Hrvoje Hudo Nov 16 '16 at 11:39
  • 4
    This answer is incorrect (for Core 2.0), see response from @BatteryBackupUnit – Jafin Jun 28 '18 at 00:36
2

Ninject does not support .Net Core, instead of this we can use dependency injection of .net core. following are the steps to implement.

  1. Go to startup.cs at public void ConfigureServices(IServiceCollection services)
  2. Add services.AddTransient<Interface, Class>();
  3. Go to the controller where you want to apply dependency injection.
  4. Create a global private Interface _propertyName;
  5. Pass the interface type variable to the constructor like

 public Constructor(Interface name)
 {
     _propertyName= name;
 }
  1. Now you can access the members of the class through _propertyName.
Stuart.Sklinar
  • 3,683
  • 4
  • 35
  • 89
Salman Lone
  • 1,526
  • 2
  • 22
  • 29