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!



Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds