Guest User

Untitled

a guest
Feb 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. public class SubscriptionExtension : IExtension<ServiceHostBase>
  2. {
  3. public SubscriptionExtension(SubscriptionEndpoint subscription)
  4. {
  5. Subscription = subscription;
  6. }
  7.  
  8. #region IExtension<ServiceHostBase> Members
  9.  
  10. public void Attach(ServiceHostBase owner)
  11. {
  12. ServiceHost = owner;
  13. owner.Opened += owner_Opened;
  14. owner.Closing+= owner_Closed;
  15. }
  16.  
  17. public void Detach(ServiceHostBase owner)
  18. {
  19. owner.Opened -= owner_Opened;
  20. owner.Closing -= owner_Closed;
  21. ServiceHost = null;
  22. }
  23.  
  24. void owner_Opened(object sender, EventArgs e)
  25. {
  26. Service.Use<IServiceBusManagementService>(managementService =>
  27. {
  28. bool alreadyAdded = (from s in managementService.ListSubscribers() where s.Id == Subscription.Id select s).Count() > 0;
  29. if(!alreadyAdded) managementService.Subscribe(Subscription);
  30. });
  31. }
  32.  
  33. void owner_Closed(object sender, EventArgs e)
  34. {
  35. if (UnsubscribeOnClosing)
  36. {
  37. Service.Use<IServiceBusManagementService>(managementService =>
  38. {
  39. managementService.Unsubscribe(Subscription.Id);
  40. });
  41. }
  42. }
  43.  
  44. public SubscriptionEndpoint Subscription
  45. {
  46. get;
  47. set;
  48. }
  49.  
  50. public bool UnsubscribeOnClosing
  51. {
  52. get;
  53. set;
  54. }
  55.  
  56. ServiceHostBase ServiceHost
  57. {
  58. get;
  59. set;
  60. }
  61.  
  62.  
  63. #endregion
  64. }
  65.  
  66. public class AutoSubscribe : Attribute, IServiceBehavior
  67. {
  68. public AutoSubscribe()
  69. {
  70. UnsubscribeOnClosing = true;
  71. }
  72.  
  73. public AutoSubscribe(string name, string configurationName, Type contractType)
  74. : this()
  75. {
  76. Name = name;
  77. ConfigurationName = configurationName;
  78. ContractType = contractType;
  79. }
  80.  
  81. public AutoSubscribe(Guid subscriptionId, string name, string configurationName, Type contractType)
  82. : this()
  83. {
  84. SubscriptionId = subscriptionId;
  85. Name = name;
  86. ConfigurationName = configurationName;
  87. ContractType = contractType;
  88. }
  89.  
  90.  
  91. #region IServiceBehavior Members
  92.  
  93.  
  94. public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
  95. {
  96.  
  97. Dispatcher dispatcher;
  98.  
  99. if (DispatcherType != null)
  100. {
  101. dispatcher = (Dispatcher)Activator.CreateInstance(DispatcherType);
  102. }
  103. else
  104. {
  105. dispatcher = new WcfDispatcher();
  106. }
  107.  
  108. SubscriptionEndpoint subscription = new SubscriptionEndpoint(SubscriptionId ?? Guid.NewGuid(), Name, ConfigurationName, Address ?? serviceDescription.Endpoints[0].Address.Uri.ToString(), ContractType, dispatcher, WcfDispatcher.CreateMessageFilter(ContractType), Transient);
  109. SubscriptionExtension extension = new SubscriptionExtension(subscription);
  110. extension.UnsubscribeOnClosing = UnsubscribeOnClosing;
  111. serviceHostBase.Extensions.Add(extension);
  112. }
  113.  
  114.  
  115. public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
  116. {
  117.  
  118. }
  119.  
  120. public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
  121. {
  122. if (Address == null)
  123. {
  124. // Make sure the endpoint contains an address if we are going to default to the first.
  125. if (serviceDescription.Endpoints.Count == 0)
  126. {
  127. throw new InvalidOperationException("The service description does not contain any endpoints");
  128. }
  129. }
  130. }
  131.  
  132. #endregion
  133.  
  134.  
  135. /// <summary>
  136. /// Gets or sets the Name used for the subscription.
  137. /// </summary>
  138. public string Name
  139. {
  140. get;
  141. set;
  142. }
  143.  
  144. /// <summary>
  145. /// Gets or sets the ConfigurationName used by the subscription
  146. /// </summary>
  147. public string ConfigurationName
  148. {
  149. get;
  150. set;
  151. }
  152.  
  153. /// <summary>
  154. /// Gets or sets the type of the contract to be used by the subscription.
  155. /// </summary>
  156. public Type ContractType
  157. {
  158. get;
  159. set;
  160. }
  161.  
  162. /// <summary>
  163. /// Gets or sets the address to be used by the subscription.
  164. /// </summary>
  165. /// <remarks> If null, the address of the first endpoint in the service description will be used.</remarks>
  166. public string Address
  167. {
  168. get;
  169. set;
  170. }
  171.  
  172. /// <summary>
  173. /// Gets or sets whether the subscription should be transient
  174. /// </summary>
  175. public bool Transient
  176. {
  177. get;
  178. set;
  179. }
  180.  
  181.  
  182. public bool UnsubscribeOnClosing
  183. {
  184. get;
  185. set;
  186. }
  187.  
  188. /// <summary>
  189. /// Gets or sets the ID used by the subscription.
  190. /// </summary>
  191. /// <remarks>If null, a new GUID will be generated.</remarks>
  192. public Guid? SubscriptionId
  193. {
  194. get;
  195. set;
  196. }
  197.  
  198. /// <summary>
  199. /// Gets or sets the Dispatcher type used by the subscription.
  200. /// </summary>
  201. /// <remarks>If null, WcfDispatcher will be used</remarks>
  202. public Type DispatcherType
  203. {
  204. get;
  205. set;
  206. }
  207.  
  208. }
Add Comment
Please, Sign In to add comment