Davita
Davita

Reputation: 9144

Modules with null or empty names are not supported. Ninject

In our MVC project, we are extensively using Ninject. For an unknown reason, StandardKernel started to throw NotSupportedException with message "Modules with null or empty names are not supported". Here's the code:

        var _kernel = new StandardKernel(); // <-- this line throws exception
        _kernel.Bind<ISessionFactory>().ToMethod(x =>
            {
                 // some code...
            }).InSingletonScope();
        _kernel.Bind<IAppDomainSetup>().To<AppDomainSetup>();

anyone has any idea what could be wrong? Thanks.

P.S. here's nuget config to see assembly versions

  <package id="Ninject" version="3.0.1.10" targetFramework="net45" />  
  <package id="Ninject.MVC3" version="3.0.0.6" targetFramework="net45" />   
  <package id="Ninject.Web.Common" version="3.0.0.7" targetFramework="net45" />

and here's stacktrace

  at Ninject.KernelBase.Load(IEnumerable`1 m) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 207
   at Ninject.KernelBase.Load(IEnumerable`1 assemblies) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 245
   at Ninject.Modules.CompiledModuleLoaderPlugin.LoadModules(IEnumerable`1 filenames) in c:\Projects\Ninject\ninject\src\Ninject\Modules\CompiledModuleLoaderPlugin.cs:line 82
   at Ninject.Modules.ModuleLoader.LoadModules(IEnumerable`1 patterns) in c:\Projects\Ninject\ninject\src\Ninject\Modules\ModuleLoader.cs:line 60
   at Ninject.KernelBase.Load(IEnumerable`1 filePatterns) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 236
   at Ninject.KernelBase..ctor(IComponentContainer components, INinjectSettings settings, INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 97
   at Ninject.KernelBase..ctor(INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 57
   at Ninject.StandardKernel..ctor(INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\StandardKernel.cs:line 46
   at TradeNet.Web.MvcApplication.CreateKernel() in c:\Users\dpopiashvili.DEA\Documents\Visual Studio 2010\Projects\TradeNet\TradeNet.Web\Global.asax.cs:line 65
   at Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:line 50
   at Ninject.Web.Common.NinjectHttpApplication.Application_Start() in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\NinjectHttpApplication.cs:line 80

Upvotes: 4

Views: 591

Answers (1)

nemesv
nemesv

Reputation: 139778

When creating a StandardKernel if the LoadExtensions proerty is true (this is the default value) of the NinjectSettings class then Ninject will scan the application directory to load modules from the extensions dll.

By default it searches the following patterns:

"Ninject.Extensions.*.dll",
"Ninject.Web*.dll"

and loads all the INinjectModule implementation from the found dlls.

In your case this automatic module loading process throwing the exception because one of the loaded module's Name property is null.

This can happen in two cases:

  • Ninject loaded a module which explicitly implemented the INinjectModule and returned null as the name or a module which derived from NinjectModule but overridden the default Name implementation and returned null
  • Or the default Name implementation in the NinjectModule returned null... however this is how the source code of the Name look like:

    public virtual string Name
    {
         get { return GetType().FullName; }
    }
    

    so it only returns null if the Type.FullName returns null which can happen in very rare cases.

So to solve your problem you need to find out which extension/module has the Name null:

  • you can check the loaded Ninject/Extension dlls with fusion log to make sure that you are loading the correct ones
  • you can try-catch the new StandardKernel() and log out the loaded modules with

     try
     {
         _kernel = new StandardKernel();
     }
     catch
     {
        var modules =
            AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(a => a.GetExportedTypes()
                .Where(t => typeof(INinjectModule).IsAssignableFrom(t)))
                .ToArray();
     }
    

Until you find out which INinjectModule causing the problem you can turn off the automatic extension loading and manually load the MvcModule with

var _kernel = new StandardKernel(
     new NinjectSettings() { LoadExtensions = false }, new MvcModule());

Upvotes: 4

Related Questions