gpt4 book ai didi

asp.net-mvc-4 - HttpContextBase 如何向 ninject 注册?我没有明确绑定(bind)它

转载 作者:行者123 更新时间:2023-12-01 18:06:12 28 4
gpt4 key购买 nike

我在 NinjectWebCommon.RegisterServices 方法中创建了 HttpContextBase 的绑定(bind),但是当我尝试在 Controller 或服务中引用它时,我收到一条错误消息。

这是绑定(bind):

kernel.Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(HttpContext.Current)).InRequestScope();

错误消息如下:

Error activating HttpContextBase
More than one matching bindings are available.
Activation path:
2) Injection of dependency HttpContextBase into parameter abase of constructor of type HomeController
1) Request for HomeController

Suggestions:
1) Ensure that you have defined a binding for HttpContextBase only once.

如果我删除绑定(bind),那么它似乎会执行我想要的操作(解析为 HttpContextWrapper),但我想知道它是如何注册的?

最佳答案

but I'm wondering how this gets registered?

MvcModule.cs的源码您的问题将立即得到解答:

this.Kernel.Bind<HttpContext>().ToMethod(ctx => HttpContext.Current).InTransientScope();
this.Kernel.Bind<HttpContextBase>().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InTransientScope();

关于asp.net-mvc-4 - HttpContextBase 如何向 ninject 注册?我没有明确绑定(bind)它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962722/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com