0

I have a wrapper around a WCF service APIs

Class APIWrapper
{
   private WCFClient _client;
   //constructor opens a client connection
   public APIWrapper()
   {
      _client = new WCFClient();
      _client.open();
   }

   public API1()
   {
       _client.doSomething1();
   }

   public API2()
   {
       _client.doSomething2();
   }

}

I want to ask:

Q1 will timeout exception occur? if this wrapper class instance exists for too long? (does the WCF connection by default keepalive? without setting that attribute in config) for example, after a wrapper class is constructed, its API1 or API2 is called after 10mins, which is longer than the timeout value of this WCF connection.

Q2 Do I need explicitly close the connection, if so, should I do it in the destructor of the wrapper class like below?

~APIWrapper
{
    if(_client !=null)
    try{
         _client.close();  }
    catch(Exception e){
         _client.Abort();   }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Li Tian Gong
  • 393
  • 1
  • 6
  • 16

2 Answers2

4

I am not sure why you wanna do that, but if the WCF is hosted in a IIS7, the WCF will start with or without connections, there is no point keeping a connection alive.

In my experience, those kind of services works best when are stateless (unless you have a really good reason). I strongly suggest opening and closing the connection each time. If you are doing this for performance, there are another ways to avoid closing and opening each time.

Q1: According to MSDN the openTimeout is 1 minute

http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.binding.opentimeout.aspx

Q2: you do not need to close the connection explicitly, but it's a good practice and I strongly recommend to do it. Non closing the connections could lead in a overhead on the WCF.

Jordi
  • 2,789
  • 1
  • 20
  • 35
  • About Q1, Taner is right, you should check "inactivityTimeout" – Jordi Nov 08 '12 at 09:41
  • thanks. So for Q2, it is better to have a destructor to release the connection – Li Tian Gong Nov 08 '12 at 11:42
  • yes, always release the resources. WCF can end with lot's of non-used opened connections, leading to a performance issue. – Jordi Nov 08 '12 at 11:49
  • but WCF ClientBase implements IDisposable, when GC works, it will call .close() on it, the only different to do it explicitly is to catch the exception during close() and then call abort(). but this exception doesn't matter already during GC, don't have to catch it. – Li Tian Gong Nov 08 '12 at 22:22
  • it's a matter of care about the resources. An expensive resource should be taken as late as possible and should be released as soon as possible. Of course you can rely on GC, and probably will work for you. I come from C++ and I really like to dispose everything explicity :) – Jordi Nov 09 '12 at 07:06
2

As I know, WCF do no keep the connection alive. After a predefined time passes (inactivityTimeout="00:10:00"), the connection will throw an exception when you try to call _client.doSomething1() or any other method on the service.

WCF inactivity timeout

To keep connection alive you should call a simple method at predefined intervals, lets say every 1 minute.

However, I agree with Jordi about that you should use wcf services stateless until it is realy necessary.

Community
  • 1
  • 1
fofik
  • 998
  • 11
  • 17
  • Thanks. I think 10 mins is enough for everything. This wrapper class won't last longer than that. The APIs are stateless even when they are within the wrapper class, which is just to save the trouble of opening and closing WCF conn manually. – Li Tian Gong Nov 08 '12 at 11:37