Developing an Application
Applies To: Service Bus for Windows Server 1.1
Before starting the tutorial, please ensure that you have installed and configured the Service Bus for Windows Server correctly as described in Installing and Configuring Service Bus for Windows Server2.
To make sure that you have a service namespace
In the PowerShell command prompt, run the following command to validate that you have at least one service namespace defined:
get-SBNamespace
Example output from this command would be as follows:
Name : myServiceNamespaceName AddressingScheme : Path CreatedTime : 2/29/2012 11:35:09 PM IssuerName : myServiceNamespaceName IssuerUri : myServiceNamespaceName ManageUsers : {list of user accounts} Uri : ServiceIdentifier : PrimarySymmetricKey : myKey SecondarySymmetricKey :
Note
For more information about service namespaces, see Service Namespaces1.
In the PowerShell command prompt, run the following command to export the Service Bus for Windows Server client configuration file:
get-sbClientConfiguration > sbclient.config
Create a new project in Visual Studio
In Visual Studio 2012, create a new console application.
In Solution Explorer, right-click References, then click Add Reference. Click to add System.Runtime.Serialization item.
Add a reference to the Service Bus for Windows Server SDK
In Solution Explorer, right-click References, then click Manage NuGet Packages. This option only appears if you have the NuGet extension installed.
In the Search box, type Service Bus 1.1. Click the Service Bus 1.1 (for Windows Server) item. Complete the installation and close this dialog.
Connect to the Service Bus for Windows Server using a connection string and configuration file
The Service Bus for Windows Server enables you to specify a connection string in a configuration file and use it in your code without specifying additional parameters. This option is supported by the Service Bus for Windows Server and provides flexibility in deployment. By using connection strings, you can switch from the Service Bus for Windows Server (on-premises) to the Microsoft Azure Service Bus (cloud) by changing a setting in the configuration file.
In the PowerShell command prompt, run the following command to retrieve the connection string to your service namespace:
get-SBClientConfiguration
Example output from this command would be as follows:
Endpoint=sb://yourHost/ServiceBusDefaultNamespace;StsEndpoint=https://yourHost:9355/ServiceBusDefaultNamespace;RuntimePort=9354;ManagementPort=9355
Open the application configuration file or create one, and then paste the connection string. For example:
<configuration> <appSettings> <add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://yourHost/ServiceBusDefaultNamespace;StsEndpoint=https://yourHost:9355/ServiceBusDefaultNamespace;RuntimePort=9354;ManagementPort=9355" /> </appSettings> </configuration>
Note that if you have multiple service namespaces, you must select and copy only one of the endpoints generated by the PowerShell cmdlet.
Create NamespaceManager and MessagingFactory objects, as you would for the Microsoft Azure Service Bus.
MessagingFactory messagingFactory = MessagingFactory.Create(); NamespaceManager namespaceManager = NamespaceManager.Create();
When you are connected to the Service Bus, you can create messaging entities (queues, topics and subscriptions) using the namespace manager instance or create QueueClient, TopicClient, or SubscriptionClient objects to send and receive messages.
Connect to the Service Bus for Windows Server by providing connection parameters explicitly
In addition to using connection strings in a configuration file to connect to the Service Bus, there are situations in which you need to explicitly specify the input parameters required to connect to the Service Bus. The Service Bus introduces a helper class 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).
string ServerFQDN = "localhost"; int HttpPort = 9355; int TcpPort = 9354; string ServiceNamespace = "ServiceBusDefaultNamespace";
In your code, use the ServiceBusConnectionStringBuilder helper class to connect to the Service Bus. For example:
ServiceBusConnectionStringBuilder connBuilder = new ServiceBusConnectionStringBuilder(); connBuilder.ManagementPort = HttpPort; connBuilder.RuntimePort = TcpPort; connBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace }.Uri); connBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = ServerFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri); MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString()); NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());
For more information about connection strings for the Service Bus for Windows Server, see Configuration Connection Strings
See Also
Development How To1
Configuration Connection Strings
Service Bus Queues, Topics, and Subscriptions