Last active
December 21, 2015 12:18
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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