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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds