Skip to content

Instantly share code, notes, and snippets.

@corydolphin
Created November 21, 2012 06:54
Show Gist options
  • Save corydolphin/4123480 to your computer and use it in GitHub Desktop.
Save corydolphin/4123480 to your computer and use it in GitHub Desktop.
Fix .NET Multi-Tier Application Using Service Bus Queues. The original solution incorrectly hard codes the name for the Queue to "OrdersQueue" when creating the messaging client.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.ServiceBus.Messaging;
using Microsoft.ServiceBus;
namespace FrontendWebRole
{
public static class QueueConnector
{
// Thread-safe. Recommended that you cache rather than recreating it
// on every request.
public static QueueClient OrdersQueueClient;
// Obtain these values from the Management Portal
public const string Namespace = "your service busnamespace"; //do not include the .servicebus.windows.net extension
public const string IssuerName = "issuer name";
public const string IssuerKey = "issuer key";
// The name of your queue
public const string QueueName = "OrdersQueue";
public static NamespaceManager CreateNamespaceManager()
{
// Create the namespace manager which gives you access to
// management operations
var uri = ServiceBusEnvironment.CreateServiceUri(
"sb", Namespace, String.Empty);
var tP = TokenProvider.CreateSharedSecretTokenProvider(
IssuerName, IssuerKey);
return new NamespaceManager(uri, tP);
}
public static void Initialize()
{
// Using Http to be friendly with outbound firewalls
ServiceBusEnvironment.SystemConnectivity.Mode =
ConnectivityMode.Http;
// Create the namespace manager which gives you access to
// management operations
var namespaceManager = CreateNamespaceManager();
// Create the queue if it does not exist already
if (!namespaceManager.QueueExists(QueueName))
{
namespaceManager.CreateQueue(QueueName);
}
// Get a client to the queue
var messagingFactory = MessagingFactory.Create(
namespaceManager.Address,
namespaceManager.Settings.TokenProvider);
OrdersQueueClient = messagingFactory.CreateQueueClient(QueueName); //Create queueclient for our queue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment