在Web API 2中将Ninject依赖项注入WebApiConfig 2
是否有可能使用Ninject注入依赖到WebApiConfig类?
这是我的WebApiConfig类。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Services.Replace(typeof(IExceptionHandler), new ErrorHandlerMessageHandler(*NEEDS DEPENDENCY*));
}
}
这是我的NinjectHttpApplication声明
public class WebApiApplication : NinjectHttpApplication
{
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
return kernel;
}
private void RegisterServices(IKernel kernel)
{
//bindings
}
}
最后,我不必这样做,但我已经创建了一篇关于如何在此处完成此任务的博客文章
链接地址: http://www.djcxy.com/p/23559.html上一篇: Injecting Ninject dependencies into WebApiConfig in Web API 2