MVC 控制器的问题 + 中等信任度的依赖注入 (Ninject)

发布于 2024-07-16 04:18:12 字数 1016 浏览 16 评论 0原文

我想在中等信任环境中使用依赖注入。 为了这个目标,我选择了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果没结果 2024-07-23 04:18:13

尝试在创建容器时设置此选项:

UseReflectionBasedInjection = true;

Try to set this option when creating your container:

UseReflectionBasedInjection = true;
如何视而不见 2024-07-23 04:18:13

很好的提示(但您的意思是将其设置为 false 吗?),我更改了我的构造函数,使其如下所示:

public NinjectControllerFactory(params IModule[] modules)
        {
            _kernel = new StandardKernel(modules);
            _kernel.Options.UseReflectionBasedInjection = false;
        }

这会得到相同的异常。

我也尝试像这样覆盖 GetControllerInstance

  protected override IController GetControllerInstance(Type controllerType)
    {
        return _kernel.Get(controllerType) as IController;
    }

但这会抛出一个安全异常,因为它使用反射。

Good tip(but did you mean set it to false?), I changed my constructor so that its like this:

public NinjectControllerFactory(params IModule[] modules)
        {
            _kernel = new StandardKernel(modules);
            _kernel.Options.UseReflectionBasedInjection = false;
        }

This gets the same exception though.

I also tried overriding GetControllerInstance like this too

  protected override IController GetControllerInstance(Type controllerType)
    {
        return _kernel.Get(controllerType) as IController;
    }

But this throws an security exception because it uses reflection.

我不咬妳我踢妳 2024-07-23 04:18:12

要以中等信任度运行 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 to true. 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.

与风相奔跑 2024-07-23 04:18:12

我最终没能解决这个问题,我切换到 Unity - 在中等信任度下工作,没有任何麻烦。

I never managed to resolved this issue in the end, I switched to Unity - works in medium trust without any hassle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文