Guest User

Untitled

a guest
Feb 20th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. public class WcfClient<TServer> where TServer : class
  2. {
  3. private Binding _binding;
  4. private EndpointAddress _endpoint;
  5.  
  6. public WcfClient(Binding binding, EndpointAddress endpoint)
  7. {
  8. this._binding = binding;
  9. this._endpoint = endpoint;
  10. }
  11.  
  12. public void Call(Action<TServer> function)
  13. {
  14. TServer client = null;
  15. try
  16. {
  17. client = new ChannelFactory<TServer>(this._binding, this._endpoint).CreateChannel();
  18. function(client);
  19. }
  20. catch
  21. {
  22. throw;
  23. }
  24. finally
  25. {
  26. if (client != null)
  27. {
  28. IClientChannel channel = (IClientChannel)client;
  29. if (channel.State == CommunicationState.Faulted)
  30. {
  31. channel.Abort();
  32. }
  33. else
  34. {
  35. channel.Close();
  36. }
  37. }
  38. }
  39. }
  40.  
  41. public T Call<T>(Func<TServer, T> function)
  42. {
  43. TServer client = null;
  44. try
  45. {
  46. client = new ChannelFactory<TServer>(this._binding, this._endpoint).CreateChannel();
  47. return (function(client));
  48. }
  49. catch
  50. {
  51. throw;
  52. }
  53. finally
  54. {
  55. if (client != null)
  56. {
  57. IClientChannel channel = (IClientChannel)client;
  58. if (channel.State == CommunicationState.Faulted)
  59. {
  60. channel.Abort();
  61. }
  62. else
  63. {
  64. channel.Close();
  65. }
  66. }
  67. }
  68. }
  69. }
Add Comment
Please, Sign In to add comment