Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

New Features in WCF 4.5 - Part 5

, 29 Apr 2014
Rate this:
Please Sign up or sign in to vote.
"A ChannelFactory basically creates a Channel for WCF clients to communicate with WCF service endpoint".In one of my previous WCF Interview Questions Tutorial Series, we discussed about the difference between using ChannelFactory and Windows Communication Foundation Proxy. Here in this WCF Tutorial,
"A ChannelFactory basically creates a Channel for WCF clients to communicate with WCF service endpoint".
In one of my previous WCF Interview Questions Tutorial Series, we discussed about the difference between using ChannelFactory and Windows Communication Foundation Proxy. Here in this WCF Tutorial, we are going to discuss about a new feature of Windows Communication Foundation v 4.5 i.e. "ChannelFactory Cache". WCF 4.5 now support for caching Channel Factories to reduce overhead associated while creating a new ChannelFactory instance.
Before moving forward, let's first summarize that what we have covered so far in this WCF 4.5 Tutorial Series.
Using ChannelFactory is a good option, if we have control over Client as well as Server because in case of ChannelFactory, we have local interfaces (WCF Contract) on client to describe our WCF service.

Following is a simple code for creating a ChannelFactory:
static void Main(string[] args)
{
    BasicHttpBinding cacheTestBinding = new BasicHttpBinding();
    EndpointAddress cacheTestEndpoint = new
                                     EndpointAddress("http://localhost:XXX/CacheTestService.svc");

    ChannelFactory<ICacheTestService> cacheTestChannelFac = new
                                     ChannelFactory<ICacheTestService>(cacheTestBinding, cacheTestEndpoint);


    var objChannel = cacheTestChannelFac.CreateChannel();


     Console.WriteLine(objChannel.MethodA());


      objChannel.Close();
 }
Although using ChannelFactory in Windows Communication Foundation is a nice approach but there is resource overhead involved in creating ChannelFactory instance. To avoid this overhead and boost performance, WCF developers were using some custom caching approach for ChannelFactory.

Now with WCF 4.5, built-in support available for ChannelFactory Cache through CacheSetting property of ClientBase<T> class. Possible values of CacheSetting property are:
  • System.ServiceModel.CacheSetting.AlwaysOn
  • System.ServiceModel.CacheSetting.Default
  • System.ServiceModel.CacheSetting.AlwaysOff

CacheSetting.AlwaysOn:
Caching is always ON meaning all instances of ClientBase<TChannel> within same app-domain will be using same ChannelFactory.
ClientBase<ICacheTestService>.CacheSetting = System.ServiceModel.CacheSetting.AlwaysOn;

CacheSetting.AlwaysDefault:
With default means that only instances of ClientBase<TChannel>created from configuration file endpoint will participate in caching. Others created programmatically will not participate. Also condition of same app-domain is also applicable here.
ClientBase<ICacheTestService>.CacheSetting = System.ServiceModel.CacheSetting.Default;

CacheSetting.AlwaysOff: 
No caching for all instances of ClientBase<TChannel>.
ClientBase<ICacheTestService>.CacheSetting = System.ServiceModel.CacheSetting.AlwaysOff;

Undoubtedly, this new feature in Windows Communication Foundation v4.5 is really helpful for WCF developers to manage support for caching ChannelFactory instances.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Imran Abdul Ghani
Software Developer (Senior)
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 30 Apr 2014
Article Copyright 2014 by Imran Abdul Ghani
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid