Dan Rigsby - Coding Up Style

Developer.Speaker.Blogger

Wcf Clients: To call Open() or not to call Open()?

Posted by Dan Rigsby on February 18th, 2008

You could directly call Open() in a ChannelFactory<> like:

System.ServiceModel.ChannelFactory<IContactService> factory =
    new System.ServiceModel.ChannelFactory<IContactService>(
        "netTcp");
factory.Open();
IContactService contactService = factory.CreateChannel();
 

Or from a proxy like so:

ContactService.ContactServiceClient client =
    new ContactService.ContactServiceClient();
client.Open();
 
However, you don’t have to explicitly call Open() on your proxy or channel from your client to the server.   You could just create the client, make your requests to the server, and never call Open() yourself. If you don’t call it, then the first request you make the the server will implicitly call Open() behind the scenes.
 
Advantages for calling Open() explicitly:
  1. You can specify the Timeout that will be used when trying to open the connection.  If you let Open() be called implicitly then the default timeout will be used.
  2. Your initial calls to the server will be faster since you have already opened the connection.  Users expect to have to wait a couple of seconds for an application to start up, but once its up in running, it should be as fast and responsive as possible.

Advantages for letting Open() be called implicitly:

  1. Applications start up faster.
  2. If you never make a call to the Wcf service, then you never had to create the connection and have saved resources.

So, if you know your application will always make at least one call to the service or if there is a very good chance it will, then you should always call Open() explicitly.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>