I want to use dependency injection in an medium trust environment. To that aim I picked Ninject as Iv been told its light weight. How do I set-up injection into the controllers?
When I tried to create a custom controller factory:
public class NinjectControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel;
public NinjectControllerFactory(params IModule[] modules)
{
_kernel = new StandardKernel(modules);
}
public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
IController controller = base.CreateController(requestContext, controllerName);
_kernel.Inject(controller);
return controller;
}
}
But I'm encountering this error:
System.InvalidOperationException was unhandled by user code Message="An error occurred while creating a controller of type 'xxx'.
If the controller doesn't have a controller factory, ensure that it has a parameterless public constructor."
Any idea how to get Ninject or any other IoC framework working under medium trust (meaning no use of reflection)