. , , , , IDisposable ClientBase.
WCF - , , , IDisposable . if/else , Dispose.
, , Service Proxy Helper, .
:
public static class Service<TProxy>
where TProxy : ICommunicationObject, IDisposable, new()
{
public static void Using(Action<TProxy> action)
{
TProxy proxy = new TProxy();
bool success = false;
try
{
action(proxy);
proxy.Close();
success = true;
}
finally
{
if (!success)
{
proxy.Abort();
}
}
}
}
:
Service<MyServiceClient>.Using(svc => svc.PerformOperation());
:
Service<MyServiceClient>.Using(svc =>
{
var result = svc.PerformOperation();
ProcessResult(result);
});
. WCF , , ( ). , .