Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. //Server interface
  2. [ServiceContract(CallbackContract = typeof(IGUIService))]
  3. public interface IServerService
  4. {
  5. [OperationContract(IsOneWay = true)]
  6. void Subscribe();
  7.  
  8. [OperationContract(IsOneWay = true)]
  9. void Unsubscribe();
  10.  
  11. ...
  12.  
  13. }
  14.  
  15. //Client interface
  16. [ServiceContract]
  17. public interface IGUIService
  18. {
  19. [OperationContract(IsOneWay = true)]
  20. void MessageCallback(string Message);
  21. }
  22.  
  23. [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
  24. public class ServerService : IServerService
  25. {
  26. internal event SettingsUpdatedHandler SettingsUpdated;
  27. internal delegate void SettingsUpdatedHandler(ServerService adminPort, EventArgs e);
  28.  
  29. private static List<IGUIService> Clients = new List<IGUIService>();
  30.  
  31. public void Subscribe()
  32. {
  33. IGUIService callback = OperationContext.Current.GetCallbackChannel<IGUIService>();
  34. if(!Clients.Contains(callback))
  35. Clients.Add(callback);
  36. }
  37.  
  38. public void Unsubscribe()
  39. {
  40. IGUIService callback = OperationContext.Current.GetCallbackChannel<IGUIService>();
  41. if (Clients.Contains(callback))
  42. Clients.Remove(callback);
  43. }
  44. }
  45.  
  46. internal void SendMessage(string message)
  47. {
  48. for(int i=Clients.Count - 1; i >= 0; i--)
  49. {
  50. IGUIService callback = Clients[i];
  51. if (((ICommunicationObject)callback).State == CommunicationState.Opened)
  52. callback.MessageCallback(message);
  53. else
  54. Clients.RemoveAt(i);
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement