如何添加可以用作温莎城堡类型参数'TImpl'的依赖关系? [英] How can I add a Dependency that can be used as type parameter 'TImpl' for Castle Windsor?

本文介绍了如何添加可以用作温莎城堡类型参数'TImpl'的依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了带有特定URL的运行时异常:

I was getting this runtime exception with a particular URL:

"缺少依赖项.组件NRBQ.Web.Controllers.DeliveryController对SeaStore.Data.Legacy.Interfaces.INRBQDeliveryRepository有依赖关系,无法解决.请确保该依赖关系已正确地注册在容器中.服务,或作为内联参数提供." ExceptionType:"Castle.MicroKernel.Resolvers.DependencyResolverException "

"Missing dependency. Component NRBQ.Web.Controllers.DeliveryController has a dependency on SeaStore.Data.Legacy.Interfaces.INRBQDeliveryRepository, which could not be resolved. Make sure the dependency is correctly registered in the container as a service, or provided as inline argument." ExceptionType: "Castle.MicroKernel.Resolvers.DependencyResolverException"

...因此我将此代码(基于有效的现有代码)添加到了IOC类中:

...so I added this code (based on existing code that works) to the IOC class:

_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);

...但是那甚至不能编译;我现在得到:

...but that won't even compile; I now get:

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

我知道这可能有点神秘(我可能会忽略此问题中的一些重要细节),但是我不确定应该添加哪些其他线索.

I know this is probably a bit myseterious (I'm probably omitting some important details in this question), but I'm not sure what additional clues I should add.

响应DeliveryController是否实现INRBQDeliveryRepository:实际上,存在三种控制器类型的东西; NRBQ.Web中面向用户的浏览器:

In response to whether DeliveryController implements INRBQDeliveryRepository: Actually, there are three controller-type things; a user-facing one in 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中的中间人:

...then a middle one in 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中进行幕后繁重工作的人:

....and finally the one that really does the behind-the-scenes heavy lifting, in 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();
        }

最后一个电话指的是这里:

That last call refers to here:

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

这是我最好的尝试,它是复制现有的测试/示例代码,但是我承认我的头在游动,我并不真正了解发生了什么(如何以及为什么).

This all is my best attempt at copying existing test/sample code, but I admit that my head is swimming and I don't really grok what's happening (how and why).

尽管我很确定它还是不是弥敦道的热狗(它似乎在整个创作过程中都在追逐自己,首先调用这种抽象,然后调用一个抽象,然后调用另一个抽象,然后再返回到第一个,然后再调用另一个,然后再返回到第二个,依此类推.依次类推,依次类推.依次添加以下内容:

Although I'm pretty sure it's still not quite a Nathan's hot dog (it seems to chase itself all over creation, calling first this abstraction, then that one, then another, then back to the first, then another, then back to the second, etc. etc. ad infinitum ad nauseum advillium), adding this:

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);
    }
}

...摆脱了错误的msg并返回了我所期望的伪造结果(或者,希望的是,不是真正期望的).

...got rid of the err msg and returned the fake result I was expecting (or, rather, hoping for, not really expecting).

推荐答案

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

In your IWindsorInstaller unit, register the interface and its implementer:

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

这篇关于如何添加可以用作温莎城堡类型参数'TImpl'的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆