Castle Windsor WCF Facility Register all Clients with Custom ClientModel

607 views Asked by At

I have a set of interfaces that I'd like to register as WCF clients in Windsor and have them all use WCF discovery to find endpoints. I was hoping to do something as simple as this:

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

Where WcfDiscoveryClientModel is defined like this:

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

But of course, no code in WcfDiscoveryClientModel is ever executed. From looking at the facility source, it looks like it'll only ever use DefaultClientModel unless I pass something into the arguments to Resolve(). I'm not entirely certain what I'd pass in as arguments, but I'd really like to avoid that route since these clients will be used in a web app.

So, my question is: what is the best way to override the default client model selection?

1

There are 1 answers

0
thorphin On

I think you might be on the wrong path, assuming that your service is setup to be discoverable all you need to do is tell your client endpoint to discover the service. Here is an example:

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