Skip to content

Instantly share code, notes, and snippets.

@DotNetNerd
Created May 24, 2013 08:53
Show Gist options
  • Save DotNetNerd/5642220 to your computer and use it in GitHub Desktop.
Save DotNetNerd/5642220 to your computer and use it in GitHub Desktop.
Basic sample of using Azure service bus
/* Init */
string connectionString = "Endpoint=sb://xxx.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=xxx";//CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
/* Setup */
if (!namespaceManager.TopicExists("TestTopic"))
{
var td = new TopicDescription("TestTopic")
{
MaxSizeInMegabytes = 5120,
DefaultMessageTimeToLive = new TimeSpan(0, 10, 0)
};
namespaceManager.CreateTopic(td);
}
if (!namespaceManager.SubscriptionExists("TestTopic", "HighMessages"))
{
var highMessagesFilter = new SqlFilter("MessageNumber > 3");
namespaceManager.CreateSubscription("TestTopic", "HighMessages", highMessagesFilter);
}
/* Send */
TopicClient senderClient = TopicClient.CreateFromConnectionString(connectionString, "TestTopic");
var sendMessage = new BrokeredMessage("Weee this is cool");
sendMessage.Properties["MyAppSpecificProp"] = "SomeProp";
sendMessage.Properties["MessageNumber"] = 4;
senderClient.Send(sendMessage);
/* Receive */
var recieveClient = SubscriptionClient.CreateFromConnectionString(connectionString, "TestTopic", "HighMessages");
recieveClient.Receive();
while (true)
{
BrokeredMessage message = recieveClient.Receive();
if (message != null)
{
try
{
Console.WriteLine("Body: " + message.GetBody<string>());
Console.WriteLine("MessageID: " + message.MessageId);
Console.WriteLine("App prop: " + message.Properties["MyAppSpecificProp"]);
Console.WriteLine("MessageNumber: " + message.Properties["MessageNumber"]);
message.Complete();
}
catch (Exception)
{
message.Abandon();
}
}
}
//namespaceManager.DeleteTopic("TestTopic");
//namespaceManager.DeleteSubscription("TestTopic", "HighMessages");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment