如何添加可用作类型参数的依赖项' TImpl'温莎城堡?

时间:2014-06-23 23:09:45

标签: asp.net-web-api dependency-injection castle-windsor ioc-container dependency-resolver

我使用特定的URL获取此运行时异常:

缺少依赖关系。组件NRBQ.Web.Controllers.DeliveryController依赖于SeaStore.Data.Legacy.Interfaces.INRBQDeliveryRepository,无法解析。请确保依赖项在容器中正确注册为服务,或作为内联论据提供。“ ExceptionType:“Castle.MicroKernel.Resolvers.DependencyResolverException

...所以我将此代码(基于现有的代码)添加到IOC类:

_container.Register
     (Component
    .For<INRBQDeliveryRepository>()
    .ImplementedBy<DeliveryController>()
    .LifeStyle.Transient);

在某些情况下:

private static Castle.Windsor.IWindsorContainer _container;

_container = new Castle.Windsor.WindsorContainer();
_container.AddFacility<Castle.Facilities.FactorySupport.FactorySupportFacility>();

. . .

_container.Register
  (Component
  .For<INRBQDeliveryRepository>()
  .ImplementedBy<DeliveryController>()
  .LifeStyle.Transient);

......但这甚至都不会编译;我现在得到:

类型'NRBQ.API.Controllers.DeliveryController'不能在泛型类型或方法'Castle.MicroKernel.Registration.ComponentRegistration.ImplementedBy()'中用作类型参数'TImpl'。从'NRBQ.API.Controllers.DeliveryController'到'SeaStore.Data.Legacy.Interfaces.INRBQDeliveryRepository'没有隐式引用转换。

我知道这可能有点神秘(我可能在这个问题中省略了一些重要的细节),但我不确定我应该添加哪些额外的线索。

更新

响应DeliveryController是否实现了INRBQDeliveryRepository:实际上,有三种控制器类型的东西; NRBQ.Web中面向用户的一个:

public class DeliveryController : ApiController
{
    private readonly INRBQDeliveryRepository _deliveryRepository;

    public DeliveryController(INRBQDeliveryRepository deliveryRepository)
    {
        if (deliveryRepository == null)
        {
            throw new ArgumentNullException("DeliveriesController");
        }
        _deliveryRepository = deliveryRepository;
    }

    [Route("api/Deliveries/Count")] 
    public int GetCountOfDeliveryRecords()
    {
        return _deliveryRepository.GetCount();
    }
    . . .

...然后是NRBQ.Client中的中间人:

namespace NRBQ.Client
{
    public class RESTNRBQDelivery : INRBQDelivery
    {
        INRBQClientSettings NRBQClientSettings;
        IRESTAPIClient RESTAPIClient;

        public RESTNRBQDelivery(IRESTAPIClient RESTAPIClient, INRBQClientSettings NRBQClientSettings)
        {
            this.NRBQClientSettings = NRBQClientSettings;
            this.RESTAPIClient = RESTAPIClient;
        }

        public RESTNRBQDelivery(IRESTAPIClient RESTAPIClient, INRBQClientSettings NRBQClientSettings, AuthenticationHeaderValue AuthHeader)
        {
            this.NRBQClientSettings = NRBQClientSettings;
            this.RESTAPIClient = RESTAPIClient;
            this.RESTAPIClient.AuthHeader = AuthHeader;
        }

        public int GetCount()
        {
            throw new NotImplementedException(); //TODO: Implement
        }

....最后是在NRBQ.API中真正做幕后工作的人:

namespace NRBQ.API.Controllers
{
    public class DeliveryController : ApiController
    {
        ILogger Logger;
        INRBQService NRBQService;

        public DeliveryController(ILogger Logger, INRBQService NRBQService)
        {
            this.NRBQService = NRBQService;
            this.Logger = Logger;
        }

        [HttpGet]
        [Route("api/Deliveries/Count")] 
        public int GetCountOfDeliveryRecords()
        {
            //return _deliveryRepository.GetCount();
            return NRBQService.GetNRBQEntity();
        }

最后一次电话是指:

public int GetNRBQEntity()
{
    return 17; // Bogus val for now
}

这一切都是我复制现有测试/示例代码的最佳尝试,但我承认我的头脑正在游泳,我并不真正了解发生了什么(如何以及为什么)。

更新2

虽然我很确定它仍然不是Nathan的热狗(它似乎追逐自己的所有创造,首先调用这个抽象,然后是那个抽象,然后是另一个,然后回到第一个,然后是另一个,然后再回来到了第二个等等,无限广告,并添加了这个:

container.Register
 (Component
 .For<INRBQDelivery>()
 .ImplementedBy<RESTNRBQDelivery>()
 .LifeStyle.Transient);

...在上下文中:

public class NRBQClientInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register
         (Component
         .For<SeaStore.Common.HTTP.IRESTAPIClient>()
         .ImplementedBy<SeaStore.Common.HTTP.WebAPIClient>()
         .LifeStyle.Transient);

        container.Register
         (Component
         .For<INRBQClient>()
         .ImplementedBy<RESTNRBQClient>()
         .LifeStyle.Transient);

        container.Register
         (Component
         .For<INRBQDelivery>()
         .ImplementedBy<RESTNRBQDelivery>()
         .LifeStyle.Transient);
    }
}

...摆脱了错误的消息,并返回了我期待的假结果(或者,更确切地说,希望,并不是真的期待)。

1 个答案:

答案 0 :(得分:0)

在IWindsorInstaller单元中,注册接口及其实现者:

container.Register
 (Component
 .For<INRBQDelivery>()
 .ImplementedBy<RESTNRBQDelivery>()
 .LifeStyle.Transient);