Guest User

Untitled

a guest
Feb 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. public class WcfManagementService : RuntimeService
  2. {
  3. protected override void OnStart()
  4. {
  5. base.OnStart();
  6.  
  7. _host = new ServiceHost(new ServiceBusManagementService(Runtime));
  8. _host.Open();
  9.  
  10. }
  11.  
  12. ServiceHost _host;
  13.  
  14. protected override void OnStop()
  15. {
  16. base.OnStop();
  17. if(_host != null) _host.Close();
  18. _host = null;
  19. }
  20. }
  21.  
  22. static class ServiceBusManagementServiceTypeProvider
  23. {
  24. public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
  25. {
  26. List<Type> knownTypes = new List<Type>();
  27. foreach (KnownTypeAttribute a in provider.GetCustomAttributes(typeof(KnownTypeAttribute), true).Cast<KnownTypeAttribute>())
  28. {
  29. if(a.Type != null)
  30. {
  31. knownTypes.Add(a.Type);
  32. }
  33. }
  34. knownTypes.AddRange(MessageDelivery.GetKnownTypes());
  35. return knownTypes.ToArray();
  36. }
  37. }
  38.  
  39. [ServiceContract]
  40. [ServiceKnownType("GetKnownTypes", typeof(ServiceBusManagementServiceTypeProvider))]
  41. public interface IServiceBusManagementService
  42. {
  43. [OperationContract(Action = WcfManagementServiceActions.Subscribe)]
  44. void Subscribe([MessageParameter(Name = "SubscriptionEndpoint")] SubscriptionEndpoint subscription);
  45.  
  46. [OperationContract(Action = WcfManagementServiceActions.Unsubscribe)]
  47. [FaultContract(typeof(SubscriptionNotFoundFault))]
  48. void Unsubscribe([MessageParameter(Name = "SubscriptionID")] Guid subscriptionId);
  49.  
  50. [OperationContract(Action = WcfManagementServiceActions.Listen)]
  51. void Listen([MessageParameter(Name = "ListenerEndpoint")] ListenerEndpoint endpoint);
  52.  
  53. [OperationContract(Action= WcfManagementServiceActions.StopListening)]
  54. [FaultContract(typeof(ListenerNotFoundFault))]
  55. void StopListening([MessageParameter(Name = "ListenerID")] Guid listenerId);
  56.  
  57. [OperationContract(Action = WcfManagementServiceActions.ListListeners, ReplyAction = WcfManagementServiceActions.ListListenersResponse)]
  58. Collection<ListenerEndpoint> ListListeners();
  59.  
  60. [OperationContract(Action = WcfManagementServiceActions.ListSubscribers, ReplyAction = WcfManagementServiceActions.ListSubscribersResponse)]
  61. Collection<SubscriptionEndpoint> ListSubscribers();
  62. }
  63.  
  64. [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
  65. public class ServiceBusManagementService : IServiceBusManagementService
  66. {
  67. public ServiceBusManagementService(ServiceBusRuntime runtime)
  68. {
  69. Runtime = runtime;
  70. }
  71. #region IServiceBusManagementService Members
  72.  
  73. public ServiceBusRuntime Runtime { get; private set; }
  74.  
  75. [OperationBehavior]
  76. public void Subscribe(SubscriptionEndpoint subscription)
  77. {
  78. Runtime.Subscribe(subscription);
  79. }
  80.  
  81. [OperationBehavior]
  82. [FaultContract(typeof(SubscriptionNotFoundFault))]
  83. public void Unsubscribe(Guid subscriptionId)
  84. {
  85. try
  86. {
  87. Runtime.Unsubscribe(subscriptionId);
  88. }
  89. catch (SubscriptionNotFoundException)
  90. {
  91. throw new FaultException<SubscriptionNotFoundFault>(new SubscriptionNotFoundFault(subscriptionId));
  92. }
  93. }
  94.  
  95. [OperationBehavior]
  96. public void Listen([MessageParameter(Name = "Endpoint")] ListenerEndpoint endpoint)
  97. {
  98. Runtime.AddListener(endpoint);
  99. }
  100.  
  101. [OperationBehavior]
  102. public void StopListening(Guid listenerId)
  103. {
  104. try
  105. {
  106. Runtime.RemoveListener(listenerId);
  107. }
  108. catch (ListenerNotFoundException)
  109. {
  110. throw new FaultException<ListenerNotFoundFault>(new ListenerNotFoundFault(listenerId));
  111. }
  112. }
  113.  
  114. [OperationBehavior]
  115. public Collection<SubscriptionEndpoint> ListSubscribers()
  116. {
  117. return Runtime.ListSubscribers();
  118. }
  119.  
  120. [OperationBehavior]
  121. public Collection<ListenerEndpoint> ListListeners()
  122. {
  123. return Runtime.ListListeners();
  124. }
  125.  
  126. #endregion
  127. }
Add Comment
Please, Sign In to add comment