MVC 控制器的问题 + 中等信任度的依赖注入 (Ninject)
我想在中等信任环境中使用依赖注入。 为了这个目标,我选择了 Ninject 因为我被告知它的重量轻。 如何设置注入控制器?
当我尝试创建自定义控制器工厂时:
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;
}
}
但我遇到了此错误:
System.InvalidOperationException 未被用户代码处理 Message="创建类型为“xxx”的控制器时发生错误。
如果控制器没有 控制器工厂,确保它有 无参数公共构造函数。”
知道如何让 Ninject 或任何其他 IoC 框架在中等信任下工作(意味着不使用反射)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试在创建容器时设置此选项:
Try to set this option when creating your container:
很好的提示(但您的意思是将其设置为 false 吗?),我更改了我的构造函数,使其如下所示:
这会得到相同的异常。
我也尝试像这样覆盖 GetControllerInstance
但这会抛出一个安全异常,因为它使用反射。
Good tip(but did you mean set it to false?), I changed my constructor so that its like this:
This gets the same exception though.
I also tried overriding GetControllerInstance like this too
But this throws an security exception because it uses reflection.
要以中等信任度运行 Ninject 1.x,您必须将
UseReflectionBasedInjection
选项设置为true
。 Ninject 2.0 不受此限制。反射实际上不是中等信任度下的受保护操作,但轻量级代码生成(Ninject 默认使用它进行注入)是——至少在 .NET 3.5 SP1 之前是这样。
一旦切换到基于反射的注入,您的代码应该按预期工作。 我还鼓励您查看 Ninject.Framework.Mvc(对于 Ninject 1.x)或 Ninject.Web.Mvc(对于 Ninject 2.0)。 这些扩展可以为您处理繁重的工作。
To run Ninject 1.x in medium trust, you must set the
UseReflectionBasedInjection
option totrue
. Ninject 2.0 does not suffer from this limitation.Reflection is actually not a protected operation in medium trust, but lightweight code generation (which Ninject uses by default for injection) is -- at least before .NET 3.5 SP1.
Once you switch to reflection-based injection, your code should work as intended. I'd also encourage you to look at Ninject.Framework.Mvc (for Ninject 1.x) or Ninject.Web.Mvc (for Ninject 2.0). These extensions handle the heavy-lifting for you.
我最终没能解决这个问题,我切换到 Unity - 在中等信任度下工作,没有任何麻烦。
I never managed to resolved this issue in the end, I switched to Unity - works in medium trust without any hassle.