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...
For further actions, you may consider blocking this person and/or reporting abuse
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?
Could you show some code ?
The comments system is throwing loads of errors, it's making it hard to post :-/
I had the same issue, I added it as a parameter in the Configure method in the Startup file
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.
public class BaseController : Controller
{
public BaseController()
{
var ctx = HttpContext;
}
}
public class ValuesController: BaseController
{
public ValuesController() : base() {
}
}
HttpContext is null.
Any help is appreciated
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.
Nice walkthrough! I have an issue, when I decorate my Controllers e.g. [Authorize(Policy = "ApiUser")] it doesn't work :/ before it was working!