Error loading module 'Ninject.Web.Mvc.MvcModule' of type MvcModule
I am creating an ASP.NET MVC5 web application using Ninject.MVC5 for DI. I am trying to move the NinjectWebCommon class into a separate class library project. I was able to do this successfully using the following code:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
// Uncomment to move this file to a separate DLL
kernel.Load("MyWebApp.dll");
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
In the above code MyWebApp.dll
, this is the main host name of the web application. This works, but requires me to hardcode the link to the web application DLL. In the official documentation, I read that the following can be used to automatically load all DLLs into a solution that has been split in this way:
kernel.Load(AppDomain.CurrentDomain.GetAssemblies());
However, if I use this code instead of a hardcoded link, I get the following exception (caught and thrown on the line throw
above):
An exception of type 'System.NotSupportedException' occurred in
MyWebApp.Infrastructure.dll and wasn't handled before a managed/native boundary
Additional information: Error loading module 'Ninject.Web.Mvc.MvcModule' of
type MvcModule
Can anyone provide some information on what is going on here and how to prevent this exception while avoiding hard referencing other assemblies?
source to share
When you build your Kernel, you need to make sure the parameter is LoadExtensions
set to false .
According to the documentation:
LoadExtensions: Gets or sets a value that indicates whether the kernel should automatically load extensions at startup.
Next line:
var kernel = new StandardKernel();
... should become like this:
var kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = false });
source to share