code_junkie

Renewing a WCF client when SCT has expired

Nov 13th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. using System;
  2. using System.ServiceModel;
  3. using System.ServiceModel.Security;
  4.  
  5. public static class Service
  6. {
  7. private static IService _service;
  8.  
  9. public static void Invoke(Action<IService> action)
  10. {
  11. try
  12. {
  13. action(_service);
  14. }
  15. catch (MessageSecurityException)
  16. {
  17. if (_service.State != CommunicationState.Faulted)
  18. {
  19. throw;
  20. }
  21.  
  22. _service.Abort();
  23. _service = CreateFreshInstance();
  24.  
  25. action(_service);
  26. }
  27. }
  28. }
  29.  
  30. public class ProxyWrapper<T> where T : ICommunicationObject
  31. {
  32. private T _service;
  33.  
  34. public ProxyWrapper()
  35. {
  36. _service = CreateNewInstance();
  37. }
  38.  
  39. public void Invoke(Action<T> action)
  40. {
  41. try
  42. {
  43. action(_service);
  44. }
  45. catch (MessageSecurityException)
  46. {
  47. if (_service.State != CommunicationState.Faulted)
  48. {
  49. throw;
  50. }
  51.  
  52. _service.Abort();
  53. _service = CreateNewInstance();
  54.  
  55. action(_service);
  56. }
  57. }
  58.  
  59. public TResult Invoke<TResult>(Func<T, TResult> func)
  60. {
  61. try
  62. {
  63. return func(_service);
  64. }
  65. catch (MessageSecurityException)
  66. {
  67. if (_service.State != CommunicationState.Faulted)
  68. {
  69. throw;
  70. }
  71.  
  72. _service.Abort();
  73. _service = CreateNewInstance();
  74.  
  75. return func(_service);
  76. }
  77. }
  78.  
  79. private T CreateNewInstance()
  80. {
  81. Type type = typeof(T);
  82. return (T)type.GetConstructor(Type.EmptyTypes).Invoke(null);
  83. }
  84. }
  85.  
  86. ProxyWrapper<ServiceClient> client = new ProxyWrapper<ServiceClient>();
  87. client.Invoke(s => s.SomeAction());
  88. int val = client.Invoke<int>(s => s.ReturnsAnInteger());
  89.  
  90. //Proxy implements this
  91. interface IMyService
  92. {
  93.  
  94. void MyMethod();
  95.  
  96. }
  97.  
  98. //Decorator 1
  99. public class MyServiceProxyRetryingDecorator : IMyService
  100. {
  101.  
  102. //This is the real proxy that might be faulted
  103. private realProxy = new RealProxy();
  104.  
  105. public void MyMethod()
  106. {
  107. ReEstablishProxyIfNecessary();
  108. //now just pass the call to the proxy, if it errors again,
  109. //do more handling or let the exception bubble up
  110. realProxy.MyMethod();
  111. }
  112.  
  113. private void ReEstablishProxyIfNecessary()
  114. {
  115. if(realProxy.CommunicationState == CommunicationState.Faulted)
  116. {
  117. realProxy.Abort();
  118. realProxy = new RealProxy();
  119. }
  120. }
  121. }
  122.  
  123. //Decorator 2
  124. public class MyServiceProxyExceptionHandlingDecorator : IMyService
  125. {
  126.  
  127. //This is the real proxy that might be faulted
  128. private realProxy = new RealProxy();
  129.  
  130. public void MyMethod()
  131. {
  132. try {realProxy.MyMethod(); }
  133. catch (ExceptionYouAreInterestedIn ex)
  134. {
  135. ReEstablishProxyIfNecessary();
  136. realProxy.MyMethod(); //do it again
  137. }
  138. }
  139.  
  140. private void ReEstablishProxyIfNecessary()
  141. {
  142. if(realProxy.CommunicationState == CommunicationState.Faulted)
  143. {
  144. realProxy.Abort();
  145. realProxy = new RealProxy();
  146. }
  147. }
  148. }
Add Comment
Please, Sign In to add comment