using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Microsoft.ServiceBus;
namespace RKiss.Tools.ServiceBusTester.Dialogs
{
public partial class PublisherDialog : Form
{
List<ServiceBusConnection> namespaces = null;
ServiceBusDefaults defaults = null;
string defaultTopic = null;
string servicebus = "servicebus.windows.net";
public PublisherDialog(List<ServiceBusConnection> listOfConnections, ServiceBusDefaults defaults)
{
InitializeComponent();
this.groupBoxSB.Enabled = false;
if (ConfigurationManager.AppSettings.AllKeys.Contains("servicebus"))
{
servicebus = ConfigurationManager.AppSettings["servicebus"];
}
this.textBoxPublisherId.Text = Guid.NewGuid().ToString();
this.namespaces = listOfConnections;
this.defaults = defaults;
this.defaultTopic = defaults.Topic;
this.comboBoxTR.SelectedIndex = 0;
}
void comboBoxNamespace_SelectedIndexChanged(object sender, EventArgs e)
{
this.RefreshWorker_Namespace();
}
private void comboBoxTR_SelectedIndexChanged(object sender, EventArgs e)
{
this.RefreshWorker_Namespace();
}
#region Public
public bool IsRelay
{
get { return this.checkBoxSB.Checked ? false : this.comboBoxTR.SelectedIndex == 1; }
}
public string NamespaceAddress
{
get { return this.checkBoxSB.Checked ? "" : string.Format("{0}://{1}.{2}", this.comboBoxTR.SelectedIndex == 1 ? "https" : "sb", this.comboBoxNamespace.Text, servicebus); }
}
public string TopicAddress
{
get { return this.checkBoxSB.Checked ? this.textBoxEndpoint.Text : string.Format("{0}://{1}.{2}/{3}", this.comboBoxTR.SelectedIndex == 1 ? "https" : "sb", this.comboBoxNamespace.Text, this.servicebus, this.comboBoxTopic.Text); }
}
public string PublisherName
{
get
{
if(this.checkBoxSB.Checked)
{
var uri = new Uri(this.textBoxEndpoint.Text.Split(new char[] { '?' }, 2)[0]);
return string.Format("{0}.{1}.{2}", uri.Host.Split('.')[0], uri.PathAndQuery.Trim('/'), this.textBoxPublisherId.Text);
}
else
return string.Format("{0}.{1}.{2}", this.comboBoxNamespace.Text, this.comboBoxTopic.Text, this.textBoxPublisherId.Text);
}
}
public ServiceBusConnection ServiceBusConnection
{
get { return this.checkBoxSB.Checked ? new ServiceBusConnection() { IssuerName="", IssuerSecret="", Namespace=""} : this.namespaces.Where(c => c.Namespace == this.comboBoxNamespace.Text).FirstOrDefault(); }
}
#endregion
private string[] GetTopics(string sbNamespace)
{
try
{
var ns = namespaces.FirstOrDefault(n => n.Namespace == sbNamespace);
if (ns == null)
throw new Exception(string.Format("Please, make a connection to the '{0}' namespace", sbNamespace));
var token = ServiceBusHelper.GetTokenProvider(ns.IssuerName, ns.IssuerSecret);
string namespaceaddress = string.Format("sb://{0}.{1}", sbNamespace, this.servicebus);
var namespaceClient = new Microsoft.ServiceBus.NamespaceManager(namespaceaddress, token);
var topics = namespaceClient.GetTopics();
return (from t in topics select t.Path).ToArray();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Get Topics", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
private string[] GetRelays(string sbNamespace)
{
try
{
var ns = namespaces.FirstOrDefault(n => n.Namespace == sbNamespace);
if (ns == null)
throw new Exception(string.Format("Please, make a connection to the '{0}' namespace", sbNamespace));
var token = ServiceBusHelper.GetTokenProvider(ns.IssuerName, ns.IssuerSecret);
string namespaceaddress = string.Format("sb://{0}.{1}", sbNamespace, this.servicebus);
var namespaceClient = new Microsoft.ServiceBus.NamespaceManager(namespaceaddress, token);
var relays = namespaceClient.GetRelaysAsync().Result;
return (from r in relays select r.Path).ToArray();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Get Relays", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
private void buttonRefresh_Click(object sender, EventArgs e)
{
if (this.checkBoxSB.Checked == false)
this.RefreshWorker_Namespace();
else
this.TestConnection_Worker();
}
private void RefreshWorker_Namespace()
{
this.comboBoxTopic.Items.Clear();
ThreadPool.QueueUserWorkItem(delegate(object state)
{
string selectedNamespace = null;
string selectedTopic = null;
this.InvokeEx(delegate()
{
selectedNamespace = this.comboBoxNamespace.Text;
selectedTopic = this.comboBoxTopic.Text;
this.checkBoxSB.Enabled = false;
this.labelErrorText.Visible = false;
this.groupBoxSB.Enabled = false;
this.buttonOK.Enabled = false;
this.buttonRefresh.Enabled = false;
this.buttonRefresh.Text = "Loading";
});
using (var progress = new ProgressButton(this, this.buttonRefresh, 1, 0))
{
if (this.comboBoxTR.SelectedIndex == 0)
{
var topics = this.GetTopics(selectedNamespace);
if (topics != null && topics.Count() > 0)
{
this.InvokeEx(delegate()
{
this.comboBoxTopic.Items.AddRange(topics);
this.comboBoxTopic.SelectedItem = this.comboBoxTopic.Items.Contains(selectedTopic) ? selectedTopic : this.comboBoxTopic.Items[0];
selectedTopic = this.comboBoxTopic.Text;
//this.textBoxEndpoint.Text = this.TopicAddress;
this.buttonOK.Enabled = this.comboBoxTopic.SelectedItem != null;
});
}
}
else if (this.comboBoxTR.SelectedIndex == 1)
{
var relays = this.GetRelays(selectedNamespace);
if (relays != null && relays.Count() > 0)
{
this.InvokeEx(delegate()
{
this.comboBoxTopic.Items.AddRange(relays);
this.comboBoxTopic.SelectedItem = this.comboBoxTopic.Items.Contains(selectedTopic) ? selectedTopic : this.comboBoxTopic.Items[0];
selectedTopic = this.comboBoxTopic.Text;
//this.textBoxEndpoint.Text = this.TopicAddress;
this.buttonOK.Enabled = this.comboBoxTopic.SelectedItem != null;
});
}
}
this.InvokeEx(delegate()
{
this.checkBoxSB.Enabled = true;
this.groupBoxSB.Enabled = true;
this.buttonRefresh.Text = "Refresh";
this.buttonRefresh.Enabled = true;
});
}
});
}
private void TestConnection_Worker()
{
ThreadPool.QueueUserWorkItem(delegate(object state)
{
string address = null;
this.InvokeEx(delegate()
{
address = this.textBoxEndpoint.Text;
this.labelErrorText.Visible = false;
this.groupBoxSB.Enabled = false;
this.buttonOK.Enabled = false;
this.buttonRefresh.Enabled = false;
this.buttonRefresh.Text = "Testing";
});
using (var progress = new ProgressButton(this, this.buttonRefresh, 1, 0))
{
string errorText = string.Empty;
try
{
var uri = new Uri(address);
if (uri.Scheme != "https")
throw new Exception("Only https is supported");
var response = ServiceBusHelper.GetResourceDescription(address);
response.EnsureSuccessStatusCode();
}
catch (IndexOutOfRangeException ex)
{
errorText = "The format of the URI could not be determined";
}
catch (Exception ex)
{
errorText = ex.InnerException == null ? ex.Message : ex.InnerException.InnerException == null ? ex.InnerException.Message : ex.InnerException.InnerException.Message;
}
this.InvokeEx(delegate()
{
this.labelErrorText.Text = errorText;
this.labelErrorText.Visible = !string.IsNullOrEmpty(errorText);
this.buttonOK.Enabled = string.IsNullOrEmpty(errorText);
this.buttonRefresh.Text = "Refresh";
this.buttonRefresh.Enabled = true;
});
}
});
}
private void checkBoxSB_CheckedChanged(object sender, EventArgs e)
{
this.labelErrorText.Visible = false;
this.buttonOK.Enabled = false;
if (this.checkBoxSB.Checked == false)
{
this.labelExample.Enabled = false;
this.groupBoxSB.Enabled = true;
this.labelEndpoint.Enabled = false;
this.textBoxEndpoint.Enabled = false;
this.comboBoxNamespace.Items.Clear();
string[] ns = (from n in namespaces select n.Namespace).ToArray<string>();
if (ns != null && ns.Count() > 0)
{
this.comboBoxNamespace.Items.AddRange(ns);
this.comboBoxNamespace.SelectedItem = ns.Contains(defaults.Namespace) ? defaults.Namespace : ns[0];
this.comboBoxNamespace.SelectedIndexChanged += new EventHandler(comboBoxNamespace_SelectedIndexChanged);
this.comboBoxTR.SelectedIndexChanged += new EventHandler(comboBoxTR_SelectedIndexChanged);
this.comboBoxTopic.Items.Clear();
this.comboBoxTopic.Items.Add(this.defaultTopic);
this.comboBoxTopic.SelectedIndex = 0;
this.buttonOK.Enabled = false;
this.RefreshWorker_Namespace();
}
else
{
MessageBox.Show("No Connection to the Service Bus", "Add Publisher", MessageBoxButtons.OK, MessageBoxIcon.Warning);
//this.DialogResult = System.Windows.Forms.DialogResult.Abort;
//this.Close();
}
}
else
{
this.labelExample.Enabled = true;
this.groupBoxSB.Enabled = false;
this.labelEndpoint.Enabled = true;
this.textBoxEndpoint.Enabled = true;
}
}
private void textBoxEndpoint_TextChanged(object sender, EventArgs e)
{
this.labelErrorText.Visible = false;
this.buttonOK.Enabled = false;
string address = ServiceBusHelper.GetAddressFromConnectionString(this.textBoxEndpoint.Text.Trim());
if (string.IsNullOrEmpty(address) == false)
this.textBoxEndpoint.Text = address;
}
}
}