繁体   English   中英

如何在Castle Windsor中配置此ClientBase类

[英]How to I configure this ClientBase class in Castle Windsor

我在Castle Windsor 3.3.0.0中配置类的用法时遇到问题

该类是这样的:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class DentalRecordServiceClient : ClientBase<IDentalRecordService>, IDentalRecordService
{
    public DentalRecordServiceClient()
    {
    }

    public DentalRecordServiceClient(string endpointConfigurationName) : base(endpointConfigurationName)
    {
    }

    public DentalRecordServiceClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress)
    {
    }

    public DentalRecordServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress)
    {
    }

    public DentalRecordServiceClient(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress)
    {
    }

    public int GetNumberOfVisits(string fullname, string city, DateTime dateOfBirth)
    {
        return base.Channel.GetNumberOfVisits(fullname, city, dateOfBirth);
    }
}

我有两个与此相关的接口,如下所示:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[ServiceContractAttribute(ConfigurationName = "IDentalRecordService")]
public interface IDentalRecordService
{
    [OperationContractAttribute(Action = "http://tempuri.org/IDentalRecordService/GetNumberOfVisits", ReplyAction = "http://tempuri.org/IDentalRecordService/GetNumberOfVisitsResponse")]
    int GetNumberOfVisits(string fullname, string city, DateTime dateOfBirth);
}

和这个:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface IDentalRecordServiceChannel : IDentalRecordService, IClientChannel
{
}

我的温莎城堡配置如下:

container.Register(
            Component.For<IDentalRecordService>()
                     .ImplementedBy<DentalRecordServiceClient>());

在我的代码中,我试图通过此行来解析服务:

var service = container.Resolve<IDentalRecordService>();

但是我收到以下错误:

Castle.MicroKernel.ComponentActivator.ComponentActivatorException:ComponentActivator:无法实例化DentalRecordServiceClient ----> System.Reflection.TargetInvocationException:调用的目标引发了异常。 ----> System.InvalidOperationException:在ServiceModel客户端配置部分中找不到引用合同'IDentalRecordService'的默认终结点元素。 这可能是因为找不到您的应用程序的配置文件,或者是因为在客户端元素中找不到与该协定匹配的端点元素。

我想我意识到我的Castle Windsor配置应该指定参数,因此调用了DentalRecordServiceClient上的其他构造函数之一,但是我不确定我需要如何或提供什么数据。 我完全可以传入伪数据,以便解析行有效。

有什么帮助吗? 提前致谢。

编辑:

好的,我发现我可以使用以下代码直接实例化该对象:

var binding = new BasicHttpBinding();
var address = new EndpointAddress("http://myaddress");
var service = new DentalRecordServiceClient(binding, address);

如何通过温莎城堡做到这一点?

TIA

自从我对WCF做任何事情已经有好几年了,但是我认为这与温莎无关。 您看到的异常( System.InvalidOperationException )是由客户端的构造函数而不是Windsor引发的。

实际上,如果要替换var service = container.Resolve<IDentalRecordService>(); 使用var service = new DentalRecordServiceClient(); 结果将是相同的。

您需要确保WCF配置正确才能起作用。

至于更新的答案,您可以按照文档中的说明使用内联依赖项配置服务。

我发现使用IOC设置WCF客户端总是有些复杂。 就我而言,我通常在创建客户端的工厂方法上注册接口。

container.Register(Component.For<IDentalRecordService>().UsingFactoryMethod(c =>
{
    var binding = new BasicHttpBinding();
    var address = new EndpointAddress("http://myaddress");
    var service = new DentalRecordServiceClient(binding, address);
    return service;
}));

也可以将IDentalRecordService直接映射到DentalRecordServiceClient并使用Krzysztof Kozmic指出的内联依赖关系,但是工厂方法一开始可能更容易理解。 不过,不要犹豫使用内联依赖项,这也很容易:

container.Register(
    Component.For<BasicHttpBinding>(),
    Component.For<EndpointAddress>().DependsOn(Dependency.OnAppSettingsValue("uri", "endpoint")),
    // you can get the value from anywhere, here it grabs it from the config file
    Component.For<IDentalRecordService>().ImplementedBy<DentalRecordServiceClient>());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM