【发布时间】:2011-03-10 09:57:12
【问题描述】:
我的 Global.aspx 中有以下代码
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new ServiceModule());
}
我还有以下 Ninject 模块:
internal class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IProductService>().To<ProductService>().InRequestScope();
}
}
我还有一个基本控制器:
public class BaseController : Controller
{
[Inject]
public IProductService ProductService
{
get;
set;
}
}
此代码有效。我遇到的问题是我想从基本控制器中删除注入属性并在 Ninject ServiceModule 中指定它。换句话说,我将如何在 ServiceModule 中编写一个绑定规则,告诉 Ninject 将 ProductService 注入基本控制器的属性中?
如果我删除该属性,我将得到 NullReferenceException。
【问题讨论】:
标签: asp.net-mvc-2 ioc-container ninject ninject-2