I'm having an issue with DynamicProxyModule and Ninject. I want to load all modules that are present in the root directory of my program (without having to write copious amounts of code), but the DynamicProxyModule doesn't work if it loaded after the Ninject Kernel is constructed. All other modules seem to work fine. I'm using this with Prism for WPF if that makes any difference. Here is my code:
public class Bootstrapper : NinjectBootstrapper
{
protected override IKernel CreateKernel()
{
var ninjectSettings = new NinjectSettings { LoadExtensions = false };
IKernel kernel = new StandardKernel(ninjectSettings);
return kernel;
}
protected override void InitializeModules()
{
// Load modules present in all local directory dll files.
Kernel.Load(new string[] {"*.dll"});
// Initialize the loaded modules
base.InitializeModules();
// Add Service class to the IoC container
Kernel.Bind<Service>().ToSelf();
// Configure Ninject to intercept any call to the Service class's
// Execute() function and replace the call with the following
// console message
Kernel.InterceptReplace<Service>(x => x.Execute(), invocation =>
{
Console.WriteLine("INTERCEPTION!");
});
// Now, request an instance of Service from the IoC container
var service = Kernel.Get<Service>();
// Call the Execute function, expecting it to be intercepted
service.Execute();
}
// More stuff for the Bootstrapper
// ...
}
public class Service
{
public virtual void Execute()
{
Console.WriteLine("Code Executed");
}
}
Interception never happens! However, if I change LoadExtensions to true and change the Kernel.Load() parameters to the code below, it works. What's going on?
var ninjectSettings = new NinjectSettings { LoadExtensions = true };
Kernel.Load("TestProject.*");
My application needs to support plugins/modules from 3rd party developers, but I won't know what the dlls will be called. I can get the dll names from the root directory or any plugins folder I create, but I was hoping there was a less verbose or more elegant way of doing it that I might have missed.
Here are the NuGet packages that I'm using:
- Prism for WPF with the associated Ninject packages (6.1.0)
- Ninject.Extensions.Conventions (3.2.0)
- Ninject.Extensions.Interception (3.2.0)
- Ninject.Extensions.Interception.DynamicProxy (3.2.0)
- Castle.Core (3.3.3)
Anyways, thank you for taking the time to read this and for any help you can give!