DCSIMG
WCF Contrib – Your own base Client Channel Classes - Zuker On Foundations

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)
WCF Contrib – Your own base Client Channel Classes

One of the core components in WCF Contrib is the ‘ClientChannel’ which handles the client side proxy creation and management, allowing you to easily call services, with the addition of environment support, using using, and comprehensive behaviors extensions model.

Currently, the built-in Client Channels can be instantiated with a configuration name which it loads the ChannelFactory with.
A common question is, how can you use the Client Channel if you like to instantiate the ChannelFactory in code.

The way to do that is to inherit from ClientChannel and override ‘CreateChannelFactoryCore’ and just return the ChannelFactory of your wish.
A more general solution, one that I recommend in every company that wants to use WCF Contrib, is to write your own base Client Channel classes to be used everywhere in your code.
This approach gives you a singular point where you can control how every Client Channel behaves and plug-in default behavior extensions.

Following is an example of such base classes. This example allows the developer to pass in a ChannelFactory to be used, that way, we enable the scenario of using a Channel Factory through code.

Code Snippet
public class MyClientChannel<T> : MyClientChannel<MyClientChannel<T>, T>
    where T : class
{
    public MyClientChannel() { }

    public MyClientChannel(ChannelManageOptions manageOptions)
        : base(manageOptions) { }

    public MyClientChannel(ChannelFactory channelFactory)
        : base(channelFactory) { }

    public MyClientChannel(ChannelFactory channelFactory, ChannelManageOptions manageOptions)
        : base(channelFactory, manageOptions) { }
}

public class MyClientChannel<TInstance, T> : ClientChannel<TInstance, T>
    where T : class
    where TInstance : ClientChannel<T>, new()
{
    private ChannelFactory _factory;

    public MyClientChannel()
    {
        OnInitialize(null);
    }

    public MyClientChannel(ChannelManageOptions manageOptions)
        : base(manageOptions)
    {
        OnInitialize(null);
    }

    public MyClientChannel(ChannelFactory channelFactory)
    {
        OnInitialize(channelFactory);
    }

    public MyClientChannel(ChannelFactory channelFactory, ChannelManageOptions manageOptions)
        : base(manageOptions)
    {
        OnInitialize(channelFactory);
    }

    void OnInitialize(ChannelFactory channelFactory)
    {
        //You can decide to disable factory caching if you provide it with a factory which is cached already
        //CacheChannelFactory = (channelFactory != null);

        _factory = channelFactory;
    }

    protected override ChannelFactory CreateChannelFactoryCore(string endpointName)
    {
        if (_factory == null)
        {
            //If the factory wasn't provided from outside, just call the base which tries to load it from the configuration
            _factory = base.CreateChannelFactoryCore(endpointName);
        }

        return _factory;
    }
}

Then, you can use it as follows -

Code Snippet
ChannelFactory<IService> myFactory = new ChannelFactory<IService>(
    new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:11000/MyService"));
using (MyClientChannel<IService> myClientChannel = new MyClientChannel<IService>(myFactory))
{
    myClientChannel.Channel.Do(null);
}

Published Sunday, October 10, 2010 3:41 PM by Amir Zuker

תגים:,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: