c#如何使用ninject为WebAPI注入对自定义处理程序的依赖
我的c#c#如何使用ninject为WebAPI注入对自定义处理程序的依赖,c#,asp.net-web-api,ninject,C#,Asp.net Web Api,Ninject,我的c#WebAPI项目有以下处理程序 public class CustomLogHandler : DelegatingHandler { [Inject] public ILogRepository LogRepository { get; set; } ... some methods here } 此处理程序已在myWebApiConfig.cs类中注册 public static class WebApiCo
WebAPI
项目有以下处理程序
public class CustomLogHandler : DelegatingHandler
{
[Inject]
public ILogRepository LogRepository { get; set; }
... some methods here
}
此处理程序已在myWebApiConfig.cs
类中注册
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
... some code here
config.MessageHandlers.Add(new CustomLogHandler());
}
}
此外,我的ILogRepository在我的NinjectWebCommon.cs
类中注册为我的其他存储库:
kernel.Bind(typeof(ILogRepository)).To(typeof(Data.LogRepository));
问题是,当我调试并且断点点击我的CustomLogHandler
方法时,LogRepository
实例为空
有线索吗 我找到了一种非常有效的方法:
var service = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(ILogRepository)) as ILogRepository;
希望这对其他人有用
谢谢也将处理程序注册到内核中,然后在将实例添加到处理程序集合之前解析该实例可能的重复