0

Why is that the that concrete WCF client implementation does implement the IDisposable but not the default out of the box interface provided for each wcf client as part of the .net?

    using(WcfClient client = new WcfClient) 
    {
         client.WebMethod();
    }


    using(IWcfClient client = new WcfClient) 
    {
         client.WebMethod();
    } //throws bc it's not part of the Interfact but is the part of the base class every wcf client implements along the interface?

Why not to make it a part of the interface as one can choose how to handle cleaning up the wcf in a custom way?

dexter
  • 7,063
  • 9
  • 54
  • 71
  • On a tangential note, be aware that mixing WCF clients with `using` blocks can mask exceptions. http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue – Matt Davis Dec 22 '10 at 17:50

1 Answers1

1

It's because the interface is the data contract of the web service and it doesn't make sense for a data contract to implement IDisposable. A data contract contains operation contracts, there's nothing to dispose. It's the channel which needs to be disposed and that's why the auto-generated proxy class (WcfClient) implements IDisposable.

You could try this if you insist on using your data contract:

IWcfClient client = new WcfClient();
using((IDisposable)client) 
{
    client.WebMethod();
}

but I really don't see what's wrong with:

using(var client = new WcfClient()) 
{
    client.WebMethod();
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yeah and how is the concrete instance of the client is not an encapsulation of the 2 end point channel. How is it different from the interface in that sense then? – dexter Dec 22 '10 at 17:04
  • @Max, the WcfClient class derives from `ClientBase` and implements the data contract. It is this `ClientBase` which encapsulates the channel and is necessary to implement IDisposable, not the data contract. – Darin Dimitrov Dec 22 '10 at 17:06