private void MonitorForm_Load(object sender, EventArgs e) { string header = string.Format("{0, -13}{1, -22}{2}", "Client", "Time", "Event"); this.listBoxExecutionProgress.Items.Add(header); _syncContext = SynchronizationContext.Current; _channelFactory = new ChannelFactory <ICalculator>("calculatorservice"); EventMonitor.MonitoringNotificationSended += ReceiveMonitoringNotification; this.Disposed += delegate { EventMonitor.MonitoringNotificationSended -= ReceiveMonitoringNotification; _channelFactory.Close(); }; ICalculator proxy = _channelFactory.CreateChannel(); (proxy as ICommunicationObject).Open(); for (int i = 1; i < 6; i++) { ThreadPool.QueueUserWorkItem(state => { int clientId = Interlocked.Increment(ref clientIdIndex); EventMonitor.Send(clientId, EventType.StartCall); using (OperationContextScope contextScope = new OperationContextScope(proxy as IContextChannel)) { MessageHeader <int> messageHeader = new MessageHeader <int>(clientId); OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader.GetUntypedHeader(EventMonitor.CientIdHeaderLocalName, EventMonitor.CientIdHeaderNamespace)); proxy.Add(1, 2); } EventMonitor.Send(clientId, EventType.EndCall); }, null); } }
public double Add(double x, double y) { EventMonitor.Send(EventType.StartExecute); Thread.Sleep(5000); double result = x + y; EventMonitor.Send(EventType.EndExecute); return(result); }
private void Form1_Load(object sender, EventArgs e) { string header = string.Format(" {0,-13} {1,-22} {2} ", "Client", "Time", "Event"); listBox1.Items.Add(header); _syncContext = SynchronizationContext.Current; _channelFactory = new ChannelFactory <ICalculator>("calculatorservice"); EventMonitor.MonitoringNotificationSended += ReceiveMonitoringNotification; this.Disposed += Form1_Disposed; ICalculator proxy = _channelFactory.CreateChannel(); (proxy as ICommunicationObject).Open(); //显式开启可以 服务调用可以及时处理 for (int i = 0; i < 5; i++) { ThreadPool.QueueUserWorkItem((state) => { #region 的代理进行调用 //int clientId = Interlocked.Increment(ref clientIndex); //ICalculator proxy = _channelFactory.CreateChannel(); //using (proxy as IDisposable) //{ // EventMonitor.Send(clientId, EventType.StartCall); // using (OperationContextScope contextScope = new OperationContextScope(proxy as IContextChannel)) // { // MessageHeader<int> messageHeader = new MessageHeader<int>(clientId); // OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader.GetUntypedHeader(EventMonitor.ClientIdHeaderLocalName, EventMonitor.ClientIdHeaderNamespace)); // proxy.Add(1, 2); // } // EventMonitor.Send(clientId, EventType.EndCall); //} #endregion int clientId = Interlocked.Increment(ref clientIndex); EventMonitor.Send(clientId, EventType.StartCall); using (OperationContextScope contextScope = new OperationContextScope(proxy as IContextChannel)) { MessageHeader <int> messageHeader = new MessageHeader <int>(clientId); OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader.GetUntypedHeader(EventMonitor.ClientIdHeaderLocalName, EventMonitor.ClientIdHeaderNamespace)); proxy.Add(1, 2); } EventMonitor.Send(clientId, EventType.EndCall); #region #endregion }, null); } }
public void Add(double x, double y) { //PreCallback EventMonitor.Send(EventType.StartExecute); Thread.Sleep(5000); double result = x + y; //Callback EventMonitor.Send(EventType.StartCallback); int clientId = OperationContext.Current.IncomingMessageHeaders.GetHeader <int>(EventMonitor.CientIdHeaderLocalName, EventMonitor.CientIdHeaderNamespace); MessageHeader <int> messageHeader = new MessageHeader <int>(clientId); OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader.GetUntypedHeader(EventMonitor.CientIdHeaderLocalName, EventMonitor.CientIdHeaderNamespace)); OperationContext.Current.GetCallbackChannel <ICalculatorCallback>().ShowResult(result); EventMonitor.Send(EventType.EndCallback); //PostCallback Thread.Sleep(5000); EventMonitor.Send(EventType.EndExecute); }
private void ConcurrentCallViaSameProxy() { ICalculator proxy = _channelFactory.CreateChannel(); (proxy as ICommunicationObject).Open(); for (int i = 1; i < 6; i++) { ThreadPool.QueueUserWorkItem(state => { int clientId = Interlocked.Increment(ref clientIdIndex); EventMonitor.Send(clientId, EventType.StartCall); using (OperationContextScope contextScope = new OperationContextScope(proxy as IContextChannel)) { MessageHeader <int> messageHeader = new MessageHeader <int>(clientId); OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader.GetUntypedHeader(EventMonitor.CientIdHeaderLocalName, EventMonitor.CientIdHeaderNamespace)); proxy.Add(1, 2); } EventMonitor.Send(clientId, EventType.EndCall); }, null); } }
public void ShowResult(double result) { EventMonitor.Send(EventType.StartExecuteCallback); Thread.Sleep(10000); EventMonitor.Send(EventType.EndExecuteCallback); }