Recently I was creating a new MVC4 application and of course I wanted to use Ninject as my IoC container. The first thing I did was to download and install the following NuGet packages:
After I got these setup I opened up the NinjectWebCommon.cs class and started to add code to the RegisterServices method. The first thing i did was to add the code below;
kernel.Bind(scanner =>
{
try
{
scanner.FromAssemblyContaining()
.SelectAllClasses()
.BindDefaultInterface();
scanner.FromAssemblyContaining()
.SelectAllClasses()
.BindDefaultInterface();
scanner.FromAssemblyContaining()
.SelectAllClasses()
.BindDefaultInterface();
}
catch (Exception e) // added to try to get the exception message
{
Debug.WriteLine(e);
throw;
}
});
The above code would step over the first scanner call w/ no issues, in fact if that was the ONLY statement it all worked as expected, however when it stepped onto the second scanner. call I would get the following error.
System.ArgumentNullException: Value cannot be null. Parameter name: first
Because I knew everything worked fine with only one scanner. call i decided to give it a go w/ creating a unique kernal.Bind for each assembly i wanted scanned as below:
kernel.Bind(scanner => scanner.FromAssemblyContaining()
.SelectAllClasses()
.BindDefaultInterface());
kernel.Bind(scanner => scanner.FromAssemblyContaining()
.SelectAllClasses()
.BindDefaultInterface());
kernel.Bind(scanner => scanner.FromAssemblyContaining()
.SelectAllClasses()
.BindDefaultInterface());
This did the trick, I could now setup the bindings for multiple assemblies with no issues. So it appears that with this version of the Extensions class for ninject you need to do each action as a unique .Bind method.
Till next time,
DIGITAL JUICE
No comments:
Post a Comment
Thank's!