Skip to content

Instantly share code, notes, and snippets.

@serbrech
Last active December 21, 2015 12:18
Show Gist options
  • Select an option

  • Save serbrech/6304498 to your computer and use it in GitHub Desktop.

Select an option

Save serbrech/6304498 to your computer and use it in GitHub Desktop.
My little wcf service call helper class to handle try catch and close/abort the proxy.
public static class ServiceCall
{
public static T Try<T>(ICommunicationObject service, Func<T> call)
{
try
{
if (service != null)
service.Open();
return call();
}
finally
{
HandleProxyClose(service);
}
}
private static void HandleProxyClose(ICommunicationObject communication)
{
if (communication != null)
{
if (communication.State == CommunicationState.Faulted)
{
communication.Abort();
}
if (communication.State != CommunicationState.Closed)
{
communication.Close();
}
}
}
}
public class ServiceFacade : IServiceFacade
{
private readonly Func<IMyService> _svcFactory;
public ServiceFacade(Func<IMyService> serviceFactory)
{
_svcFactory = serviceFactory;
}
public ResultObject MakeACall()
{
var service = _svcFactory.Invoke();
return ServiceCall.Try((ICommunicationObject) service, () =>
{
var result = service.MakeACall(new RequestObject(){/*...whatever request you need.*/});
return result;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment