Hi there

 

Creating a Service Bus Queue by code is something useful in lot of cases

Luckily the related library downloadable from NuGet give us a NamespaceManager class that appealed to us

here a simple example in c# console application

 

//the namespace manager for manipulating ServiceBus
var ns = NamespaceManager.CreateFromConnectionString(CONNECTION_STRING);

var queueName = “demoqueue1”;

Console.WriteLine(“Checking queue existance”);

if (ns.QueueExists(queueName))
    ns.DeleteQueue(queueName);

Console.Write(“Creating queue”);

var newQueueDescription = ns.CreateQueue(new QueueDescription(queueName)
    {
        MaxSizeInMegabytes = 1024,
        DefaultMessageTimeToLive = TimeSpan.FromMinutes(10),
    });

while (newQueueDescription.Status != EntityStatus.Active)
{
    Console.Write(“..”);
    Thread.Sleep(500);
}

Console.WriteLine(” OK”);

Console.WriteLine(“Opening client”);

var queueClient = QueueClient.CreateFromConnectionString(CONNECTION_STRING, queueName);

Console.Write(“Sending messages”);

for (int i = 0; i < 10; i++)
{
    var msg = new BrokeredMessage(i);
    msg.Properties[“typename”] = i.GetType().ToString();
    msg.Properties[“typeversion”] = i.GetType().Assembly.GetName().Version.ToString();
    queueClient.Send(msg);
}

Console.WriteLine(” OK”);

Console.WriteLine(“Trying dequeue…”);

while (true)
{
    var msg = queueClient.Receive(TimeSpan.FromSeconds(3));
    if (msg == null)
        break;

    var value = msg.GetBody<int>();

    Console.WriteLine(“Receiving: {0} ({1} {2})”, value, msg.Properties[“typename”], msg.Properties[“typeversion”]);

    msg.Complete();
}

Console.WriteLine(“END”);

Lascia un commento