DEV Community

Wiring up Ninject with ASP.NET Core 2.0

Ivan on November 23, 2017

This guide is for Ninject versions 3.3.x Ninject is a lightning-fast, ultra-lightweight dependency injector for .NET applications. It helps you sp...
Collapse
 
nigelgos profile image
Nigel Gossage

Nice little write up. I've hit an issue where I want my service to take in IOptions as a constructor but the standard way of adding an IOptions using DI are not working with this tutorial. Any tips?

Collapse
 
cwetanow profile image
Ivan

Could you show some code ?

Collapse
 
nigelgos profile image
Nigel Gossage

The comments system is throwing loads of errors, it's making it hard to post :-/

Thread Thread
 
lr12003 profile image
David Lopez • Edited

I had the same issue, I added it as a parameter in the Configure method in the Startup file

Collapse
 
ataraxia89 profile image
ataraxia89 • Edited

All of this works fine except for a couple of tweaks needed to use a custom DbContext. I have ApplicationDbContext, which inherits from IdentityDbContext.

Within Startup.cs, I added an IConfiguration parameter to the Configure and RegisterApplicationComponents methods (one feeds the other of course), then within RegisterApplicationComponents I added:

kernel.Bind<IConfiguration>().ToConstant(configurationVariable);

My API is running in Azure and the IConfiguration gets injected at runtime. Then, within the ApplicationDbContext class, I had to add the following constructor:

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IConfiguration configurationVariable) : base(options)

Within the constructor I call configurationVariable.GetConnectionString("MyConnectionStringName") and assign it to a private string variable "_connectionStringVariable" within the ApplicationDbContextClass. Then added the following override:

protected override void OnModelCreating(ModelBuilder builder)

...which contains a couple of bits specific to my application but most importantly calls base.OnModelCreating(builder). I then added the following override:

protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseSqlServer(_connectionStringVariable);
_connectionString = null;
base.OnConfiguring(builder);
}

The ApplicationDbContext can then be used by my internal services.

Collapse
 
tarakeshp profile image
Tarakesh Pulikonda • Edited

public class BaseController : Controller
{
public BaseController()
{
var ctx = HttpContext;
}
}

public class ValuesController: BaseController
{
public ValuesController() : base() {

}
}

HttpContext is null.
Any help is appreciated

Collapse
 
khanzzirfan profile image
Irfan Khan

Nice Article. I have also added IOptions to inject in to constructor.
Please see the gist here
github.com/ninject/Ninject/issues/270

Thank you.

Collapse
 
lr12003 profile image
David Lopez

Nice walkthrough! I have an issue, when I decorate my Controllers e.g. [Authorize(Policy = "ApiUser")] it doesn't work :/ before it was working!