简体   繁体   English

如何在Castle Windsor中配置此ClientBase类

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

I'm having trouble configuring usage of a class in Castle Windsor 3.3.0.0 我在Castle Windsor 3.3.0.0中配置类的用法时遇到问题

The class is like this: 该类是这样的:

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

I have two associated interfaces for it like this: 我有两个与此相关的接口,如下所示:

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

and this: 和这个:

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

My Castle Windsor configuration is like this: 我的温莎城堡配置如下:

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

In my code, I'm trying to resolve the service by this line: 在我的代码中,我试图通过此行来解析服务:

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

But I get the following error: 但是我收到以下错误:

Castle.MicroKernel.ComponentActivator.ComponentActivatorException : ComponentActivator: could not instantiate DentalRecordServiceClient ----> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. Castle.MicroKernel.ComponentActivator.ComponentActivatorException:ComponentActivator:无法实例化DentalRecordServiceClient ----> System.Reflection.TargetInvocationException:调用的目标引发了异常。 ----> System.InvalidOperationException : Could not find default endpoint element that references contract 'IDentalRecordService' in the ServiceModel client configuration section. ----> System.InvalidOperationException:在ServiceModel客户端配置部分中找不到引用合同'IDentalRecordService'的默认终结点元素。 This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. 这可能是因为找不到您的应用程序的配置文件,或者是因为在客户端元素中找不到与该协定匹配的端点元素。

I think I realise that my Castle Windsor config should specify parameters so one of the other constructors on DentalRecordServiceClient is called but I'm not sure how or what data I need to feed it. 我想我意识到我的Castle Windsor配置应该指定参数,因此调用了DentalRecordServiceClient上的其他构造函数之一,但是我不确定我需要如何或提供什么数据。 I'm completely ok for dummy data to be passed in just so the Resolve line works. 我完全可以传入伪数据,以便解析行有效。

Any help? 有什么帮助吗? Thanks in advance. 提前致谢。

Edit: 编辑:

Ok, I have found that I can instantiate the object directly using the following code: 好的,我发现我可以使用以下代码直接实例化该对象:

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

How can I do this via Castle Windsor? 如何通过温莎城堡做到这一点?

TIA TIA

It's been years since I've done anything with WCF but I think this has little to do with Windsor. 自从我对WCF做任何事情已经有好几年了,但是我认为这与温莎无关。 The exception you see (the System.InvalidOperationException ) is thrown by the constructor of your client, not Windsor. 您看到的异常( System.InvalidOperationException )是由客户端的构造函数而不是Windsor引发的。

In fact, if you were to replace your var service = container.Resolve<IDentalRecordService>(); 实际上,如果要替换var service = container.Resolve<IDentalRecordService>(); with var service = new DentalRecordServiceClient(); 使用var service = new DentalRecordServiceClient(); the result would be the same. 结果将是相同的。

You need to make sure your WCF configuration is correct for this to work. 您需要确保WCF配置正确才能起作用。

As for the updated answer, you can configure your service with inline dependencies as explained in the documentation . 至于更新的答案,您可以按照文档中的说明使用内联依赖项配置服务。

I find WCF clients are always a bit complicated to set up using IOC. 我发现使用IOC设置WCF客户端总是有些复杂。 In my case I usually register the interface against a factory method that creates the client. 就我而言,我通常在创建客户端的工厂方法上注册接口。

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

It is also possible to map IDentalRecordService directly to DentalRecordServiceClient and to use inline dependencies as pointed out by Krzysztof Kozmic, but factory methods may be easier to understand at first. 也可以将IDentalRecordService直接映射到DentalRecordServiceClient并使用Krzysztof Kozmic指出的内联依赖关系,但是工厂方法一开始可能更容易理解。 Don't hesitate to play with inline dependencies, though, it is quite easy too: 不过,不要犹豫使用内联依赖项,这也很容易:

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