我如何配置这个ClientBase类在温莎城堡

本文关键字:ClientBase 城堡 何配置 配置 | 更新日期: 2023-09-27 18:09:06

在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
{
}

我的Castle Windsor配置是这样的:

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

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

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

但是我得到以下错误:

Castle.MicroKernel.ComponentActivator。ComponentActivator:无法实例化DentalRecordServiceClient----> System.Reflection.TargetInvocationException:异常已经被调用的目标抛出。——>系统。在ServiceModel客户端配置部分中,无法找到引用契约"IDentalRecordService"的默认端点元素。这可能是因为没有找到您的应用程序的配置文件,或者因为在客户端元素中没有找到匹配此契约的端点元素。

我想我意识到我的温莎城堡配置应该指定参数,所以DentalRecordServiceClient上的其他构造器之一被调用,但我不确定如何或什么数据我需要喂养它。我完全可以将虚拟数据传入,以便Resolve行工作。

帮忙吗?提前谢谢。

编辑:

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

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

我怎么能通过温莎城堡?

TIA

我如何配置这个ClientBase类在温莎城堡

我已经好几年没有使用WCF了,但我认为这与Windsor关系不大。您看到的异常(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>());