Derik Whittaker

Syndication

News


Convention Binding to Ninject 3 -- Resolving ‘Value Cannot be Null’ Exception

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,


Posted 08-12-2012 7:26 AM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

Markus Zywitza wrote re: Convention Binding to Ninject 3 -- Resolving ‘Value Cannot be Null’ Exception
on 08-13-2012 1:29 AM

Are there some angle brackets missing in the code or why would you call the same code thrice?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)