如何解析温莎城堡中的代理类

本文关键字:代理 城堡 何解析 | 更新日期: 2023-09-27 18:04:53

我有一个WCF服务,我试图使用Castle Windsor解决。过去的注册看起来像这样:

container.Register(Component.For<IBatchDataService>()
.AsWcfClient(WCFEndpoint
.FromConfiguration("Internal.IBatchDataService"))
.LifestyeTransient())

现在我已经创建了一个驻留在进程中的代理。它公开了相同的接口(IBatchDataService),并将对WCF服务的引用作为构造函数参数。如何在Windsor中设置此设置,以便解析任何其他类以使用代理类,但代理类解析为WCF服务。我现在就有这个:

container.Register(Component.For<IBatchDataService>()
.ImplementedBy<BatchDataServiceClient>());

如何解析温莎城堡中的代理类

试试这个:

container.Register(
    Component.For<IBatchDataService>().AsWcfClient(WCFEndpoint.FromConfiguration("Internal.IBatchDataService")).LifestyeTransient().Named("wcfBatchDataService"),
    Component.For<IBatchDataService>().ImplementedBy<BatchDataServiceClient>().AsDefault().DependsOn(
        Dependency.OnComponent("constructorParameterName", "wcfBatchDataService")
)

其中constructorParameterName是构造函数中IBatchDataService参数的名称。我没有在编译器中运行它,所以请让我知道这是否适合你。

亲切的问候,Marwijn .

这只是一个装饰器模式。温莎支持它OOTB:

container.Register(
    Component.For<IBatchDataService>().
        ImplementedBy<BatchDataServiceClient>(),
    Component.For<IBatchDataService().
        AsWcfClient(WCFEndpoint.FromConfiguration("Internal.IBatchDataService")).
        LifestyleTransient());