將Ninject與MVC5和SignalR連接起來的最佳實踐方式是什麼?ASP.Net MVC 5 + SignalR + Ninject
Ninject建議有Global.asax中繼承NinjectHttpApplication和實施CreateKernal方法:
//Global.asax ([Ninject MVC Website Guidance][1])
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel(new NinjectRegistrations());
var ninjectResolver = new CustomNinjectDependencyResolver(kernel);
DependencyResolver.SetResolver(ninjectResolver); // MVC
GlobalConfiguration.Configuration.DependencyResolver = ninjectResolver; // Web API
return kernel;
}
SignalR希望我們用自己的OwinStartup類註冊NinjectDependencyResolver: 需要
//OWIN Startup ([ASP.Net Website Guidance][2])
public void Configuration(IAppBuilder app)
{
var kernel = new StandardKernel();
var resolver = new NinjectSignalRDependencyResolver(kernel);
var config = new HubConfiguration();
config.Resolver = resolver;
Microsoft.AspNet.SignalR.StockTicker.Startup.ConfigureSignalR(app, config);
}
的iKernel和NinjectDependencyResolver在這兩個地方。一種選擇是開始爲這些使用靜態單例,但這似乎並不符合我們首先使用Ninject拍攝的鬆耦合的精神。
感謝您的指點!
不要遵循的指導,這是老了。只需安裝Ninject.MVC5,ninject將自行配置。按照下面的Anders建議(但使用Ninject.MVC5而不是MVC3,儘管MVC3也可以)。 –
酷,不知道他們得到周圍升級包 – Anders