Tutorial :ChannelFactory Reuse Strategies


I've been reading that ChannelFactory creation is expensive and that unless there is a technical reason not to, one should reuse ChannelFactories when possible either by caching them someway, or by using static instances of the factories.

In your experience, what ChannelFactory reuse strategies have you found to be the most useful and robust within the context of an ASP.NET application?


If you're using .NET 3.0 SP1 and up and don't need special stuff that requires handling channels directly, then the best option would be to just use client-side proxy classes derived from ClientBase<T> (like the ones generated when importing services). Those already cache the factory underneath. See here for the details.

If not, then yeah, you'll need to stick the IChannelFactory<T> object somewhere, but you need to still make sure you handle sharing appropriately (I don't think there's any guarantees made by the stack that access of a factory is thread-safe), but other than that, it should be fairly straight-forward.


Darin Damitrov posted a useful answer around re-use of channelfactories here:-

creating WCF ChannelFactory<T>

I've been looking at memory usage and performance in an application I've been working on recently. I applied the technique he advocated (using a dictionary to store a set of channelfactories) and had a good performance bump with it.

What I was seeing was that instantiating a channelfactory could take up to 70ms. If this is happening a lot it adds up quickly.

Currently, I'm being cautious and have not set up my DI container to serve up the same instances of the channelfactories for the lifetime of the application. Instead, I make them live for the duration of a single HTTP request (within which there may be many back-end service calls made).

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »