Export (0) Print
Expand All

Configuration Connection Strings

Updated: August 18, 2014

Microsoft Azure supports a set of managed APIs that provide a consistent way to create new instances of Azure service clients (such as the Service Bus) across Microsoft cloud services. The APIs enable you to instantiate these clients (for example, CloudBlobClient, SubscriptionClient, TopicClient) regardless of where the application is hosted -- on-premises, in a Microsoft cloud service, in websites, or in a persistent VM Role. You can also use these API to retrieve the configuration information necessary for instantiating these clients, and to change the configuration without having to redeploy the calling application. The APIs are located in the CloudConfigurationManager class.

To instantiate a client (for example, a QueueClient), you can represent the configuration information as a connection string. Azure supports a CloudConfigurationManager class that retrieves the connection string from the configuration file. On the client side, for each of the different types of clients there is a method that instantiates that client type by using that connection string. For Service Bus, these methods are:

For example, given the following configuration section:

<appSettings>
    <add key="Microsoft.ServiceBus.ConnectionString" 
        value="Endpoint=sb://[namespace].servicebus.windows.net; SharedSecretIssuer=owner;SharedSecretValue=someSecret"/>
</appSettings>

The following code retrieves the connection string, creates a queue, and initializes the connection to the queue:

QueueClient Client; 

string connectionString = AzureConfigurationManager.AppSettings.GetSetting("Microsoft.ServiceBus.ConnectionString");
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString); 

if (!namespaceManager.QueueExists(QueueName))
    {
        namespaceManager.CreateQueue(QueueName);
    }

// Initialize the connection to Service Bus Queue
Client = QueueClient.CreateFromConnectionString(connectionString, QueueName);

In addition to using connection strings in a configuration file to connect to the Service Bus, there are situations in which you must explicitly specify the input parameters required to connect to Service Bus. Service Bus defines a helper class, ServiceBusConnectionStringBuilder, that enables you to build connection strings in code.

In your code, identify the connection parameters (by either using a configuration file, or some other way). Then, use the ServiceBusConnectionStringBuilder helper class to connect to Service Bus. For example, the following code creates a connection string for use with Microsoft Azure Service Bus:

var serviceConnection = new ServiceBusConnectionStringBuilder();
serviceConnection.Endpoints.Add(new Uri("sb://test-namespace.servicebus.windows.net"));
serviceConnection.SharedSecretIssuerName = "owner";
serviceConnection.SharedSecretIssuerSecret = "0dteeO3/apkXXXXXXXXXXXXXXXXXXXXXXXXXXXT2G0=";

The ServiceBusConnectionStringBuilder helper class enables you to create connection strings for several different scenarios. The following sections contain examples.

The following example builds a connection string of the form

“Endpoint=sb://fjprod-tat-001-monitor1.servicebus.wa.fj-cloud.net/;StsEndpoint=https://fjprod-tat-001-monitor1-sb.accesscontrol.wa.fj-cloud.net/;SharedSecretIssuer=owner;SharedSecretValue=0dteeO3/apkXXXXXXXXXXXXXXXXXXXXXXXXXXXT2G0=”
var serviceCustomConnection = new ServiceBusConnectionStringBuilder();
serviceCustomConnection.Endpoints.Add(new Uri("sb://fjprod-tat-001-monitor1.servicebus.wa.fj-cloud.net/"));
serviceCustomConnection.StsEndpoints.Add(new Uri("https://fjprod-tat-001-monitor1-sb.accesscontrol.wa.fj-cloud.net/"));
serviceCustomConnection.SharedSecretIssuerName = "owner";
serviceCustomConnection.SharedSecretIssuerSecret = "0dteeO3/apkXXXXXXXXXXXXXXXXXXXXXXXXXXXT2G0=";

The following example builds a connection string of the form

"Endpoint=sb://machine1/test-namespace;StsEndpoint=https://machine1:9355/test-namespace;RuntimePort=9354;ManagementPort=9355"
var serverLogonConnection = new ServiceBusConnectionStringBuilder();
serverLogonConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace"));
serverLogonConnection.StsEndpoints.Add(new Uri("https://machine1:9355/test-namespace"));
serverLogonConnection.RuntimePort = 9354;
serverLogonConnection.ManagementPort = 9355;

The following example builds a connection string of the form

"Endpoint=sb://machine1/test-namespace,sb://machine2/test-namespace;StsEndpoint=https://machine1:9355/test-namespace,https://machine2:9355/test-namespace;RuntimePort=9354;ManagementPort=9355"
var serverMultiEndpointsConnection = new ServiceBusConnectionStringBuilder();
serverMultiEndpointsConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace"));
serverMultiEndpointsConnection.Endpoints.Add(new Uri("sb://machine2/test-namespace"));
serverMultiEndpointsConnection.StsEndpoints.Add(new Uri("https://machine1:9355/test-namespace"));
serverMultiEndpointsConnection.StsEndpoints.Add(new Uri("https://machine2:9355/test-namespace"));
serverMultiEndpointsConnection.RuntimePort = 9354;
serverMultiEndpointsConnection.ManagementPort = 9355;

The following example builds a connection string of the form

"Endpoint=sb://machine1/test-namespace;StsEndpoint=https://machine1:9355/test-namespace;RuntimePort=9354;ManagementPort=9355;WindowsUsername=testUser;WindowsDomain=<Your machine name>;WindowsPassword=password"
var serverLocalUserConnection = new ServiceBusConnectionStringBuilder();
serverLocalUserConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace"));
serverLocalUserConnection.StsEndpoints.Add(new Uri("https://machine1:9355/test-namespace"));
serverLocalUserConnection.RuntimePort = 9354;
serverLocalUserConnection.ManagementPort = 9355;
serverLocalUserConnection.WindowsCredentialDomain = Environment.MachineName;
serverLocalUserConnection.WindowsCredentialUsername = "testUser";
serverLocalUserConnection.WindowsCredentialPassword = securePassword;

The following example builds a connection string of the form

"Endpoint=sb://machine1/test-namespace;StsEndpoint=https://machine1:9355/test-namespace;RuntimePort=9354;ManagementPort=9355;WindowsUsername=testUser;WindowsDomain=redmond;WindowsPassword=password"
var serverDomainConnection = new ServiceBusConnectionStringBuilder();
serverDomainConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace"));
serverDomainConnection.StsEndpoints.Add(new Uri("https://machine1:9355/test-namespace"));
serverDomainConnection.RuntimePort = 9354;
serverDomainConnection.ManagementPort = 9355;
serverDomainConnection.WindowsCredentialDomain = "redmond";
serverDomainConnection.WindowsCredentialUsername = "testUser";
serverDomainConnection.WindowsCredentialPassword = securePassword;

The following example builds a connection string of the form

"Endpoint=sb://machine1/test-namespace;RuntimePort=9354;ManagementPort=9355"
var serverAnonymousConnection = new ServiceBusConnectionStringBuilder();
serverAnonymousConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace"));
serverAnonymousConnection.RuntimePort = 9354;
serverAnonymousConnection.ManagementPort = 9355;

See Also

Show:
© 2014 Microsoft