29
|
1 using System;
|
|
2 using System.Messaging;
|
|
3 using Common.Messages;
|
|
4 using GalaSoft.MvvmLight.Messaging;
|
|
5
|
|
6 namespace Server.Listeners
|
|
7 {
|
|
8 public class MsmqListener : IListener
|
|
9 {
|
|
10 public bool IsListening { get; set; }
|
|
11
|
|
12 private readonly MessageQueue _mq;
|
|
13
|
|
14 public MsmqListener(string queueName)
|
|
15 {
|
|
16 _mq = MessageQueue.Exists(queueName)
|
|
17 ? new MessageQueue(queueName)
|
|
18 : MessageQueue.Create(queueName);
|
|
19
|
|
20 _mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
|
|
21 }
|
|
22
|
|
23 public void Start()
|
|
24 {
|
|
25 if (IsListening) return;
|
|
26
|
31
|
27 Messenger.Default.Send(new MsmqLogMessage() { Body = "Opening listener" });
|
29
|
28 IsListening = true;
|
|
29 while (IsListening)
|
|
30 {
|
|
31 try
|
|
32 {
|
|
33 var message = _mq.Receive().Body;
|
|
34 Messenger.Default.Send(new MsmqClientMessage());
|
|
35 }
|
|
36 catch (MessageQueueException mqe)
|
|
37 {
|
|
38 Messenger.Default.Send(new MsmqLogMessage() { Body = mqe.Message });
|
|
39 }
|
|
40 catch (Exception e)
|
|
41 {
|
|
42 Messenger.Default.Send(new MsmqLogMessage() { Body = e.Message });
|
|
43 }
|
|
44 }
|
|
45 }
|
|
46
|
|
47 public void Stop()
|
|
48 {
|
|
49 if (!IsListening) return;
|
|
50 _mq.Close();
|
|
51 IsListening = false;
|
31
|
52 Messenger.Default.Send(new MsmqLogMessage() { Body = "Listener closed." });
|
29
|
53 }
|
|
54 }
|
|
55 }
|