Castle Windsor WCF Facility使用Custom ClientModel注册所有客户

时间:2014-12-15 15:44:00

标签: wcf castle-windsor wcffacility

我有一组接口,我想在Windsor中注册为WCF客户端,让他们都使用WCF发现来查找端点。我希望能做到这么简单的事情:

    [TestMethod]
    public void TestMethod1()
    {
        var container = new WindsorContainer();
        container.AddFacility<WcfFacility>();
        container.Register(Component.For<IWcfClientModel>().ImplementedBy<WcfDiscoveryClientModel>());

        // NOTE: ISampleService wasn't installed on purpose 
        // to force the container to generate a WCF proxy 
        var x = container.Resolve<ISampleService>();
        Assert.IsNotNull(x);
    }

WcfDiscoveryClientModel的定义如下:

public class WcfDiscoveryClientModel : WcfClientModelBase
{
    public WcfDiscoveryClientModel()
    {
        Endpoint = WcfEndpoint.Discover();
    }
}

但当然,WcfDiscoveryClientModel中的代码都没有执行过。通过查看设施源,看起来它只会使用DefaultClientModel,除非我将某些内容传递给Resolve()的参数。我不完全确定我作为参数传递的是什么,但我真的想避开这条路线,因为这些客户端将用于网络应用程序。

所以,我的问题是:覆盖默认客户端模型选择的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

我认为您可能走错了路,假设您的服务设置为可发现,您需要做的就是告诉客户端端点发现服务。这是一个例子:

            using (var clientContainer = new WindsorContainer())
            {
                clientContainer.AddFacility<WcfFacility>();

                var b = new NetNamedPipeBinding()
                {
                    TransactionFlow = true,
                    MaxBufferPoolSize = 2147483647,
                    MaxBufferSize = 2147483647,
                    MaxReceivedMessageSize = 2147483647,
                    ReaderQuotas = new XmlDictionaryReaderQuotas
                    {
                        MaxDepth = 2147483647,
                        MaxArrayLength = 2147483647,
                        MaxStringContentLength = 2147483647,
                        MaxNameTableCharCount = 2147483647,
                        MaxBytesPerRead = 2147483647
                    }
                };

                //Notice the .Discover here, not a hard-coded address
                var endpoint = WcfEndpoint.BoundTo(b).Discover();

                clientContainer.Register(Castle.MicroKernel.Registration.Component
                              .For<IYourServiceContract>()
                              .AsWcfClient(endpoint)
                              .LifeStyle.Transient);

                var clientProxy = clientContainer.Resolve<IYourServiceContract>();

                clientProxy.SomeOperation();

                clientContainer.Release(dm);
            }