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;
}
}
このコードは機能します。私が抱えている問題は、ベースコントローラーから inject 属性を削除し、代わりに Ninject ServiceModule でこれを指定したいということです。つまり、Ninject に ProductService をベース コントローラーのプロパティに挿入するように指示するバインディング ルールを ServiceModule に記述するにはどうすればよいでしょうか。
属性を削除すると、NullReferenceException が発生します。