ChannelFactory Caching in WCF 4.5 – An Insight

In this article I will walk you through the ChannelFactory caching feature introduced as part of WCF 4.5. This article will also provide a good insight about the various caching modes available along with suitable source code samples.

Channel Factory – A Look

ChannelFactory is a factory class for creating the channel instances in a WCF client. This class simplifies the task of creating the channel between a WCF service and a client. When an instantiation is done on the client it means a channel is established with the WCF service and creates the proxy of the service for further communications like that of a method call. A channel will be created over an address, binding and contract (service endpoint) combination. Following is a sample code where ChannelFactory is used for the communication with the WCF service.

namespace ChannelFactoryCachingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelFactory<IMyWcfService> channelFactory = new ChannelFactory<IMyWcfService>("<MyServiceEndPointConfigName>");
            var channel = channelFactory.CreateChannel();
            string message = channel.GetAGreetingMessage();
            Console.WriteLine(message);
            ((IClientChannel)channel).Close();
        }
    }
}

Channel Factory – Caching

As I mentioned in the previous section the channel will be created while creating the channel instance, it also takes a certain amount or resource overhead for achieving it on the fly. Prior to the .NET Framework 4.5 developers had to implement a custom channel factory caching mechanism but now Microsoft has introduced in build caching for ChannelFactory. WCF proxy, which is generated inherit from the class named ClientBase and this class holds the property names CacheSettings to hold the caching configuration for the ChannelFactory. Also the creation of the channel will be wrapped under the ClientBase creation.

ClientBase<IClientChannel>.CacheSetting = CacheSetting.AlwaysOn;

There are three modes of ChannelFactory caching available and they are as follows.

AlwaysOn

This mode will allow the developer to make the ChannelFactory to be cached always no matter if the endpoint or other security –sensitive properties are updated. The developer should have done proper analysis so that using this mode wouldn’t affect the service calls in any means. Mentioned below is a sample code.

class Program
{
    static void Main(string[] args)
    {
        ClientBase<IClientChannel>.CacheSetting = CacheSetting.AlwaysOn;
 
        MyWcfServiceClient client = new MyWcfServiceClient(new WSHttpBinding(), new EndpointAddress("<MyEndPointAddress>"));
        client.GetAGreetingMessage();
        //The channel will be cached for any future calls
        //This will make use of the cached channelfactory object
        client = new MyWcfServiceClient(new WSHttpBinding(), new EndpointAddress("<MyEndPointAddress>"));
        client.GetAGreetingMessage();
    }
}

Default

The Default mode will cache the ChannelFactory object and use it until any of the client’s security sensitive properties are accessed. The security sensitive properties are Endpoint, ClientCredentials and the ChannelFactory. Default mode example is as follows.

class Program
{
    static void Main(string[] args)
    {
        ClientBase<IClientChannel>.CacheSetting = CacheSetting.Default;
 
        MyWcfServiceClient client = new MyWcfServiceClient(new WSHttpBinding(), new EndpointAddress("<MyEndPointAddress>"));
        client.GetAGreetingMessage();
        //Access a security-sensitive member of the client
        UserNamePasswordClientCredential userName = client.ClientCredentials.UserName;
        //The following code will recreate the ChannelFactory object and disable the existing cache
        client = new MyWcfServiceClient(new WSHttpBinding(), new EndpointAddress("<MyEndPointAddress>"));
        client.GetAGreetingMessage();
    }
}

AlwaysOff

ChannelFactory is never cached and the instance is always newly created. Following is the source code sample.

class Program
{
    static void Main(string[] args)
    {
        ClientBase<IClientChannel>.CacheSetting = CacheSetting.AlwaysOff;
 
        MyWcfServiceClient client = new MyWcfServiceClient(new WSHttpBinding(), new EndpointAddress("<MyEndPointAddress>"));
        client.GetAGreetingMessage();
 
        //The following code will recreate the ChannelFactory object
        client = new MyWcfServiceClient(new WSHttpBinding(), new EndpointAddress("<MyEndPointAddress>"));
        client.GetAGreetingMessage();
    }
}

Though these options provide a good flexibility it is always on the developer to choose the right type of cache settings. I hope this article provided a nice insight about the ChannelFactory caching. Happy reading!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read