dexter
dexter

Reputation: 7203

WCF IDisposable design discrepency

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?

Upvotes: 0

Views: 607

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

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();
}

Upvotes: 1

Related Questions