0

我的 c#WebAPI项目有以下处理程序。

public class CustomLogHandler : DelegatingHandler
    {
        [Inject]
        public ILogRepository LogRepository { get; set; }

        ... some methods here   
     }

该处理程序已在我的WebApiConfig.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实例为空。

有什么线索吗?

4

1 回答 1

0

我找到了一种很好的方法:

var service = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(ILogRepository)) as ILogRepository;

希望这对其他人有用。

谢谢

于 2018-07-29T18:14:03.287 回答