Using using(no pun) is not recommended because it Dispose()can even throw exceptions.
Here are a few extension methods we use:
using System;
using System.ServiceModel;
public static class CommunicationObjectExtensions
{
public static void SafeClose(this ICommunicationObject communicationObject)
{
if(communicationObject.State != CommunicationState.Opened)
return;
try
{
communicationObject.Close();
}
catch(CommunicationException ex)
{
communicationObject.Abort();
}
catch(TimeoutException ex)
{
communicationObject.Abort();
}
catch(Exception ex)
{
communicationObject.Abort();
throw;
}
}
public static TResult SafeExecute<TServiceClient, TResult>(this TServiceClient communicationObject,
Func<TServiceClient, TResult> serviceAction)
where TServiceClient : ICommunicationObject
{
try
{
var result = serviceAction.Invoke(communicationObject);
return result;
}
finally
{
communicationObject.SafeClose();
}
}
}
With these two:
var client = new WebServicesClient();
return client.SafeExecute(c => c.GetThing());
source
share