private void btnSubscribe_Click(object sender, EventArgs e) { try { var dynamicMessage = ((Type)cbMessages.SelectedValue).Assembly.CreateInstance(cbMessages.SelectedValue.ToString()) as Exchange.Contracts.Message; Subscription sub; m_SubMgr = new SubscriptionServiceManager(); //m_SubMgr.SubscriptionServiceCallbackEvent += new SubscriptionServiceCallBackEventHander(exchangeSubscriptionClient_SubscriptionServiceCallbackEvent); m_SubMgr.SubscriptionServiceCallbackEvent += (s, msg) => { string msgText; msgText = "Callback>>. " + "(" + msg.message.RequestID + ") Message: " + msg.message.RequestName; if (msg.message is StatusMessage) { msgText += " - Status: " + (msg.message as StatusMessage).Status.ToString(); msgText += " - Data: " + Convert.ToString((msg.message as StatusMessage).Data); } else if (msg.message is ExchangeMessage) { msgText += " - ExchangeName: " + (msg.message as ExchangeMessage).ExchangeName; msgText += " - ExchangeData: " + Convert.ToString((msg.message as ExchangeMessage).ExchangeData); } this.Invoke(new MethodInvoker(delegate(){ lbLog.Items.Add(msgText); lbLog.Items.Add("--------------------------------------------------------------------------------------------------------------"); })); }; sub = m_SubMgr.CreateSubscription <Exchange.Contracts.Message>(dynamicMessage); m_SubMgr.Subscribe(); btnSubscribe.Text = "Subscribed"; btnSubscribe.Enabled = false; lbLog.Items.Add("Client Subscribed Successfully to the " + sub.Message.GetType().ToString() + "!!!"); } catch (Exception ex) { lbLog.Items.Add(ex.ToString()); } }
private void Subscribe(string response) { string requestID = ""; if (m_SubMgr != null) { m_SubMgr.Dispose(); } try { var xResponse = System.Xml.Linq.XDocument.Parse(response); requestID = (from n in xResponse.Descendants("ExchangeResponse") select n.Element("RequestID").Value).First(); } catch { } if (String.IsNullOrEmpty(requestID)) { return; } try { Exchange.Contracts.Message dynamicMessage; if (cbMessages.InvokeRequired) { cbMessages.Invoke(new MethodInvoker(delegate() { dynamicMessage = ((Type)cbMessages.SelectedValue).Assembly.CreateInstance(cbMessages.SelectedValue.ToString()) as Exchange.Contracts.Message; })); } else { dynamicMessage = ((Type)cbMessages.SelectedValue).Assembly.CreateInstance(cbMessages.SelectedValue.ToString()) as Exchange.Contracts.Message; } Subscription sub; m_SubMgr = new SubscriptionServiceManager(); //m_SubMgr.SubscriptionServiceCallbackEvent += new SubscriptionServiceCallBackEventHander(exchangeSubscriptionClient_SubscriptionServiceCallbackEvent); m_SubMgr.SubscriptionServiceCallbackEvent += (s, msg) => { //string msgText; _pubSubLog.Clear(); _pubSubLog.AppendFormat("{0:T}-Callback>> MessageType: {2} - RequestName: {3}\n", DateTime.Now, msg.message.RequestID, msg.message.GetType().Name, msg.message.RequestName); //msgText = "Callback>>. " + "(" + msg.message.RequestID + ") Message: " + msg.message.RequestName; if (msg.message is StatusMessage) { _pubSubLog.AppendFormat(" -Status: {0}\n", (msg.message as StatusMessage).Status.ToString()); _pubSubLog.AppendFormat(" -Data: {0}\n", (msg.message as StatusMessage).Data); } else if (msg.message is ExchangeMessage) { _pubSubLog.AppendFormat(" -ExchangeName: {0}\n", (msg.message as ExchangeMessage).ExchangeName); _pubSubLog.AppendFormat(" -ExchangeData: {0}\n", (msg.message as ExchangeMessage).ExchangeData); } this.Invoke(new MethodInvoker(delegate() { txtPubSubLog.Text += _pubSubLog.ToString(); txtPubSubLog.Text += "--------------------------------------------------------------------------------------------------------------\n"; })); }; sub = m_SubMgr.CreateSubscription(requestID); m_SubMgr.Subscribe(); this.Invoke(new MethodInvoker(delegate() { txtPubSubLog.Text = "Subscribed to " + requestID; txtPubSubLog.Text += "\n--------------------------------------------------------------------------------------------------------------\n"; })); } catch (Exception ex) { this.Invoke(new MethodInvoker(delegate() { txtPubSubLog.Text += "\n" + ex.ToString(); txtPubSubLog.Text += "\n--------------------------------------------------------------------------------------------------------------\n"; })); } }