【问题标题】:How to I configure this ClientBase class in Castle Windsor如何在 Castle Windsor 中配置此 ClientBase 类
【发布时间】:2015-08-21 21:33:36
【问题描述】:

我在 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 上的其他构造函数之一,但我不确定我需要如何或什么数据来提供它。我完全可以传入虚拟数据,以便 Resolve 行正常工作。

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

编辑:

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

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

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

TIA

【问题讨论】:

  • 好的,我发现我可以通过使用以下构造函数参数正确地实例化其中一个构造函数:'var binding = new BasicHttpBinding()' 和 'var address = new EndpointAddress("@ 987654321@);'然后做'new DentalRecordServiceClient(binding, address)'。我怎样才能在温莎城堡做同样的事情?

标签: c# castle-windsor


【解决方案1】:

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

事实上,如果你要更换你的 var service = container.Resolve&lt;IDentalRecordService&gt;();var service = new DentalRecordServiceClient(); 结果是一样的。

您需要确保您的 WCF 配置正确才能使其正常工作。

至于更新后的答案,您可以使用内联依赖项as explained in the documentation 配置您的服务。

【讨论】:

  • 我同意。我不认为它是这样的城堡,我认为我需要调用其他构造函数之一(默认构造函数除外),但我不确定要传入哪些参数(甚至是虚拟值)以允许 ServiceClient 对象构造。我同意这不是城堡,所以我把问题复杂化了。
【解决方案2】:

我发现使用 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>());

【讨论】:

    猜你喜欢
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多