1: using System;
2: using System.ServiceModel;
3: using System.Threading;
4: using System.Windows.Forms;
5: using Artech.ConcurrentServiceInvocation.Service.Interface;
6: namespace Artech.ConcurrentServiceInvocation.Client
7: { 8: public partial class MonitorForm : Form
9: { 10: private SynchronizationContext _syncContext;
11: private ChannelFactory<ICalculator> _channelFactory;
12: private static int clientIdIndex = 0;
13:
14: public MonitorForm()
15: { 16: InitializeComponent();
17: }
18:
19: private void MonitorForm_Load(object sender, EventArgs e)
20: { 21: string header = string.Format("{0, -13}{1, -22}{2}", "Client", "Time", "Event"); 22: this.listBoxExecutionProgress.Items.Add(header);
23: _syncContext = SynchronizationContext.Current;
24: _channelFactory = new ChannelFactory<ICalculator>("calculatorservice"); 25:
26: EventMonitor.MonitoringNotificationSended += ReceiveMonitoringNotification;
27: this.Disposed += delegate
28: { 29: EventMonitor.MonitoringNotificationSended -= ReceiveMonitoringNotification;
30: _channelFactory.Close();
31: };
32:
33: for (int i = 1; i <= 5; i++)
34: { 35: ThreadPool.QueueUserWorkItem(state =>
36: { 37: int clientId = Interlocked.Increment(ref clientIdIndex);
38: ICalculator proxy = _channelFactory.CreateChannel();
39: using (proxy as IDisposable)
40: { 41: EventMonitor.Send(clientId, EventType.StartCall);
42: using (OperationContextScope contextScope = new OperationContextScope(proxy as IContextChannel))
43: { 44: MessageHeader<int> messageHeader = new MessageHeader<int>(clientId);
45: OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader.GetUntypedHeader(EventMonitor.CientIdHeaderLocalName, EventMonitor.CientIdHeaderNamespace));
46: proxy.Add(1, 2);
47: }
48: EventMonitor.Send(clientId, EventType.EndCall);
49: }
50: }, null);
51: }
52: }
53:
54: public void ReceiveMonitoringNotification(object sender, MonitorEventArgs args)
55: { 56: string message = string.Format("{0, -15}{1, -20}{2}", args.ClientId, args.EventTime.ToLongTimeString(), args.EventType); 57: _syncContext.Post(state => this.listBoxExecutionProgress.Items.Add(message), null);
58: }
59: }
60: }