Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tugberkugurlu/9227918 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/9227918 to your computer and use it in GitHub Desktop.
How to Use Service Bus Queues
static void Main(string[] args)
{
// Reference: http://www.windowsazure.com/en-us/documentation/articles/service-bus-dotnet-how-to-use-queues/
// Service Bus Partitioned Queue: http://code.msdn.microsoft.com/windowsazure/Service-Bus-Partitioned-7dfd3f1f
// BrokeredMessage.ViaPartitionKey: http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.brokeredmessage.viapartitionkey.aspx
const string QueueName = "TestQueue";
string serviceBusConnStr = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
QueueDescription qd = new QueueDescription(QueueName);
qd.MaxSizeInMegabytes = 5120;
// Gets or sets the default message time to live value. This is the duration after which the message expires, starting from when the message is sent to Service Bus. This is the default value used when TimeToLive is not set on a message itself.
// Messages older than their TimeToLive value will expire and no longer be retained in the message store. Subscribers will be unable to receive expired messages.
// A message can have a lower TimeToLive value than that specified here, but by default TimeToLive is set to MaxValue. Therefore, this property becomes the default time to live value applied to messages.
qd.DefaultMessageTimeToLive = TimeSpan.FromHours(1);
NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(serviceBusConnStr);
if (namespaceManager.QueueExists(QueueName) == false)
{
namespaceManager.CreateQueue(qd);
}
QueueClient queueClient = QueueClient.CreateFromConnectionString(serviceBusConnStr, QueueName);
// Send
BrokeredMessage message = new BrokeredMessage("My message");
message.Properties["RandomGuid"] = Guid.NewGuid().ToString();
queueClient.Send(message);
// Receive
// Receive actually waits if there is no available message to receive inside the queue.
BrokeredMessage receivedMessage = queueClient.Receive();
receivedMessage.Abandon();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment