Channel Factory Caching in WCF 4.5

Today I explain what ChannelFactory caching in the WCF 4.5 framework is. With the help of this new feature in .NET, we can cache the service instance. Microsoft has introduced a new property for the ClientBase<T> class; the property name is CacheSetting. We can set the cache setting with the help of the CacheSetting enum.

Here are the values of the Enum.

Name Description
System.ServiceModel.CacheSetting.AlwaysOn All instances of the Client will use the same channel factory.
System.ServiceModel.CacheSetting.AlwaysOff All instances of the Client would use different channel factories. This is useful when each endpoint has different security requirements and it makes no sense to cache.
System.ServiceModel.CacheSetting.Default All instances of the Client would use the same channel factory except instance #4. Instance #4 would use a channel factory that is created specifically for its use. This setting would work for scenarios where a particular endpoint needs different security settings from the other endpoints of the same ChannelFactory type (in this case IService).

Here is a sample code for the Default CacheSetting.

class Program {
    static void Main(string[] args) {
        ClientBase<IService1>.CacheSetting = System.ServiceModel.CacheSetting.Default;
        using(ServiceReference1.Service1Client client = new Service1Client()) {}
    }
}

Here is a sample code for the AlwaysOn CacheSetting.

static void Main(string[] args) {
    ClientBase<IService1>.CacheSetting = System.ServiceModel.CacheSetting.AlwaysOn;
    using(ServiceReference1.Service1Client client = new Service1Client()) {}
}

Here is a sample code for the AlwaysOff CacheSetting.

static void Main(string[] args) {  
    ClientBase < IService1 > .CacheSetting = System.ServiceModel.CacheSetting.AlwaysOff;  
    using(ServiceReference1.Service1Client client = new Service1Client()) {}  
} 

Reference from


Recommended Free Ebook
Similar Articles