簡體   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