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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Mobile is introducing sweeping changes throughout your workplace. As a senior stakeholder driving mobile initiatives in your organization, you may be lost in a sea of technologies and claims from vendors promising rapid delivery of applications to your employees, customers, and partners. To help explain some of the topics you will need to be aware of, and to separate the must-haves from the nice-to-haves, this reference guide can help you with applying a mobile strategy in the context of application …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds