Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main()
  4. {
  5. ServiceBase[] ServicesToRun;
  6. ServicesToRun = new ServiceBase[]
  7. {
  8. new TestService()
  9. };
  10. ServiceBase.Run(ServicesToRun);
  11. }
  12.  
  13. public static void Start(string[] args)
  14. {
  15. // onstart code here
  16. try
  17. {
  18. ConsumeMessage();
  19.  
  20. }
  21. catch (Exception e)
  22. {
  23. LogError(e);
  24. }
  25. }
  26.  
  27. public static void ConsumeMessage()
  28. {
  29. ConnectionFactory factory = new ConnectionFactory()
  30. {
  31. HostName = "server",
  32. Port = 5672,
  33. UserName = "user1",
  34. Password = "user1",
  35. VirtualHost = "/",
  36. RequestedHeartbeat = 30,
  37. };
  38.  
  39. while (!Cancelled)
  40. {
  41. try
  42. {
  43. if (_subscription == null)
  44. {
  45. try
  46. {
  47. _connection = factory.CreateConnection();
  48. }
  49. catch (BrokerUnreachableException)
  50. {
  51. //You probably want to log the error and cancel after N tries,
  52. //otherwise start the loop over to try to connect again after a second or so.
  53. continue;
  54. }
  55.  
  56. string queueName = "test-queue";
  57. _channel = _connection.CreateModel();
  58.  
  59.  
  60. _channel.ExchangeDeclare("test-exchange", "fanout", true);
  61. _channel.QueueDeclare(queueName, true, false, false, null);
  62. _channel.QueueBind(queueName, "test-exchange", "");
  63.  
  64.  
  65. _subscription = new Subscription(_channel, queueName, false);
  66. }
  67.  
  68. BasicDeliverEventArgs args;
  69. bool gotMessage = _subscription.Next(250, out args);
  70. if (gotMessage)
  71. {
  72. if (args == null)
  73. {
  74. //This means the connection is closed.
  75. // DisposeAllConnectionObjects();
  76. continue;
  77. }
  78.  
  79. savemessage(args.Body);
  80.  
  81. _subscription.Ack(args);
  82. }
  83. }
  84. catch (OperationInterruptedException ex)
  85. {
  86. // DisposeAllConnectionObjects();
  87. }
  88. }
  89. // DisposeAllConnectionObjects();
  90. }
  91.  
  92.  
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement