views:

99

answers:

1

I have a WCF service that I consume in my code and generated as a ChannelFactory class. I know that the proper way to consume the WCF is to create the ChannelFactory (let's call this AwesomeClient), do the work, then call Close() on it. Here's my snippet:

    public static void DoSomething()
    {
        var client = new AwesomeClient();
        client.DoSomethingAwesome();
        client.Close();
    }

However, I am expecting that DoSomething will be called quite frequently (say 10 times a minute?), so the advice I have gotten is to instantiate the ChannelFactory as a static instance, and always reuse the same instance, and never having to Close it (because this is 'cheaper' than always recreating the ChannelFactory and then closing it).

I'm here for a second opinion, can anyone tell me why not calling Close and reusing the static instance is a good idea? Or should I just stick with recreating the ChannelFactory and Close()-ing it for every call?

+1  A: 

10 times a minute is not that often. 10 times a second i would definitely at least consider reusing the channel.

There is a lot of unknowns in your case to make a good decision. How many clients are going to be connecting to the service? What kind of connection is this (is there a chance it will go down for a fraction of a second), is there a load balancer? Proxy?

and by the way, if you do decide to open and close the channel every time, there is no need to recreate the ChannelFactory. Keep that static, just open and close a new channel every time. Most of the time creating a factory consumes more resources.

Vitalik
I've decided to open and close a new channel for every call. Thanks for the pointer.
Klaw