using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Threading;
using System.Windows.Forms;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
namespace RKiss.Tools.ServiceBusTester.Dialogs
{
public partial class ReceiverDialog : Form
{
List<ServiceBusConnection> namespaces = null;
string defaultQueue = null;
string servicebus = "servicebus.windows.net";
public ReceiverDialog(List<ServiceBusConnection> listOfConnections, ServiceBusDefaults defaults)
{
InitializeComponent();
if (ConfigurationManager.AppSettings.AllKeys.Contains("servicebus"))
{
servicebus = ConfigurationManager.AppSettings["servicebus"];
}
this.namespaces = listOfConnections;
this.defaultQueue = defaults.Queue;
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.comboBoxQueue.Items.Add(this.defaultQueue);
this.comboBoxQueue.SelectedIndex = 0;
this.comboBoxQueue.SelectedIndexChanged += new EventHandler(comboBoxQueue_SelectedIndexChanged);
this.buttonOK.Enabled = false;
this.RefreshWorker_Namespace();
}
else
{
MessageBox.Show("No Connection to the Service Bus", "Add Receiver", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.DialogResult = System.Windows.Forms.DialogResult.Abort;
this.Close();
}
}
#region Public
public string NamespaceAddress
{
get { return string.Format("sb://{0}.{1}", this.comboBoxNamespace.Text, this.servicebus); }
}
public string ReceiverName
{
get { return string.Format("{0}.{1}", this.comboBoxNamespace.Text, this.comboBoxQueue.Text); }
}
public string ReceiverAddress
{
get { return string.Format("sb://{0}.{1}/{2}", this.comboBoxNamespace.Text, this.servicebus, this.comboBoxQueue.Text);}
}
public ServiceBusConnection ServiceBusConnection
{
get { return this.namespaces.Where(c => c.Namespace == this.comboBoxNamespace.Text).FirstOrDefault(); }
}
public bool RequiresSession
{
get { return this.checkedListBoxQueueOptions.CheckedItems == null ? false : this.checkedListBoxQueueOptions.CheckedItems.Contains("RequiresSession"); }
}
public string ReceiverType
{
get { return this.comboBoxType.Text; }
}
#endregion
void comboBoxNamespace_SelectedIndexChanged(object sender, EventArgs e)
{
this.RefreshWorker_Namespace();
}
void comboBoxQueue_SelectedIndexChanged(object sender, EventArgs e)
{
this.RefreshWorker_Queue();
}
private string[] GetQueues(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 = TokenProvider.CreateSharedSecretTokenProvider(ns.IssuerName, ns.IssuerSecret);
string namespaceaddress = string.Format("sb://{0}.{1}", sbNamespace, this.servicebus);
var namespaceClient = new Microsoft.ServiceBus.NamespaceManager(namespaceaddress, token);
var queues = namespaceClient.GetQueues();
return (from q in queues select q.Path).ToArray();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Get Queues", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
private QueueDescription GetQueueDescription(string sbNamespace, string queue)
{
try
{
if (string.IsNullOrEmpty(queue))
throw new Exception("Please select a Queue");
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 = TokenProvider.CreateSharedSecretTokenProvider(ns.IssuerName, ns.IssuerSecret);
string namespaceaddress = string.Format("sb://{0}.{1}", sbNamespace, this.servicebus);
var namespaceClient = new Microsoft.ServiceBus.NamespaceManager(namespaceaddress, token);
return namespaceClient.GetQueue(queue);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Get QueueDescription", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
private void buttonRefresh_Click(object sender, EventArgs e)
{
this.RefreshWorker_Namespace();
}
private void RefreshWorker_Namespace()
{
ThreadPool.QueueUserWorkItem(delegate(object state)
{
string selectedNamespace = null;
string selectedQueue = null;
this.InvokeEx(delegate()
{
selectedNamespace = this.comboBoxNamespace.Text;
selectedQueue = this.comboBoxQueue.Text;
this.groupBoxSB.Enabled = false;
this.buttonOK.Enabled = false;
this.buttonRefresh.Enabled = false;
this.buttonRefresh.Text = "Loading";
this.comboBoxQueue.Items.Clear();
});
using (var progress = new ProgressButton(this, this.buttonRefresh, 1, 0))
{
var topics = this.GetQueues(selectedNamespace);
if (topics != null && topics.Count() > 0)
{
this.InvokeEx(delegate()
{
this.comboBoxQueue.Items.AddRange(topics);
this.comboBoxQueue.SelectedItem = this.comboBoxQueue.Items.Contains(selectedQueue) ? selectedQueue : this.comboBoxQueue.Items[0];
selectedQueue = this.comboBoxQueue.Text;
});
}
else
{
this.InvokeEx(delegate()
{
this.checkedListBoxQueueOptions.Items.Clear();
this.textBoxLockDuration.Clear();
this.textBoxMessageCount.Clear();
this.textBoxMessageDeliveryCount.Clear();
this.textBoxMaxSizeInMB.Clear();
});
}
this.InvokeEx(delegate()
{
this.groupBoxSB.Enabled = true;
this.buttonRefresh.Text = "Refresh";
this.buttonRefresh.Enabled = true;
});
}
});
}
private void RefreshWorker_Queue()
{
ThreadPool.QueueUserWorkItem(delegate(object state)
{
string selectedNamespace = null;
string selectedQueue = null;
this.InvokeEx(delegate()
{
selectedNamespace = this.comboBoxNamespace.Text;
selectedQueue = this.comboBoxQueue.Text;
this.groupBoxSB.Enabled = false;
this.buttonOK.Enabled = false;
this.buttonRefresh.Enabled = false;
this.buttonRefresh.Text = "Loading";
this.checkedListBoxQueueOptions.Items.Clear();
this.textBoxLockDuration.Clear();
this.textBoxMessageCount.Clear();
this.textBoxMessageDeliveryCount.Clear();
this.textBoxMaxSizeInMB.Clear();
});
using (var progress = new ProgressButton(this, this.buttonRefresh, 1, 0))
{
var queueDescription = this.GetQueueDescription(selectedNamespace, selectedQueue);
this.InvokeEx(delegate()
{
this.checkedListBoxQueueOptions.Items.Add("RequiresSession", queueDescription.RequiresSession);
this.checkedListBoxQueueOptions.Items.Add("EnableBatchedOperations", queueDescription.EnableBatchedOperations);
this.checkedListBoxQueueOptions.Items.Add("RequiresDuplicateDetection", queueDescription.RequiresDuplicateDetection);
this.checkedListBoxQueueOptions.Items.Add("EnableDeadLetteringOnMessageExpiration", queueDescription.EnableDeadLetteringOnMessageExpiration);
this.textBoxLockDuration.Text = queueDescription.LockDuration.ToString();
this.textBoxMessageCount.Text = queueDescription.MessageCount.ToString();
this.textBoxMessageDeliveryCount.Text = queueDescription.MaxDeliveryCount.ToString();
this.textBoxMaxSizeInMB.Text = queueDescription.MaxSizeInMegabytes.ToString();
this.buttonOK.Enabled = this.comboBoxQueue.SelectedItem != null;
});
this.InvokeEx(delegate()
{
this.groupBoxSB.Enabled = true;
this.buttonRefresh.Text = "Refresh";
this.buttonRefresh.Enabled = true;
});
}
});
}
/*
private void buttonRefresh_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate(object state)
{
string selectedNamespace = null;
string selectedQueue = null;
this.InvokeEx(delegate()
{
selectedNamespace = this.comboBoxNamespace.Text;
selectedQueue = this.comboBoxQueue.Text;
this.comboBoxNamespace.SelectedIndexChanged -= new EventHandler(comboBoxNamespace_SelectedIndexChanged);
this.groupBoxSB.Enabled = false;
this.buttonOK.Enabled = false;
this.buttonRefresh.Enabled = false;
this.buttonRefresh.Text = "Loading";
this.comboBoxQueue.Items.Clear();
});
using (var progress = new ProgressButton(this, this.buttonRefresh, 1, 0))
{
var queues = this.GetQueues(selectedNamespace);
if (queues != null && queues.Count() > 0)
{
this.InvokeEx(delegate()
{
this.comboBoxQueue.Items.AddRange(queues);
this.comboBoxQueue.SelectedItem = selectedQueue;
this.buttonOK.Enabled = this.comboBoxQueue.SelectedItem != null;
});
}
this.InvokeEx(delegate()
{
this.groupBoxSB.Enabled = true;
this.buttonRefresh.Text = "Refresh";
this.buttonRefresh.Enabled = true;
this.comboBoxNamespace.SelectedIndexChanged += new EventHandler(comboBoxNamespace_SelectedIndexChanged);
});
}
});
}
*/
}
}