How to: Configure a Service Bus Client Using a Configuration File
Updated: June 18, 2014
Once you have defined and implemented your Microsoft Azure Service Bus interface, you can configure the client application. You can configure your application programmatically or in an App.config file. Configuring your application in an App.config file lets you easily see what the configuration settings are, and lets users modify the settings after deployment. Note that configuring a Service Bus application by using a configuration file is very similar to configuring a Windows Communication Foundation (WCF) application, which is discussed in Configuring Services Using Configuration Files in the Windows Communication Foundation (WCF) documentation. Therefore, the following discussion is a simplified overview of configuration, with an emphasis on the unique features relevant to Service Bus.
The main difference between configuring a service and a client is that the client must know what configuration settings the service is using, and match them. These settings typically include what type of binding to use, and also what security protocols you must have to access the service (that is, authentication and transport-level security). For more information about setting security, see Securing and Authenticating a Service Bus Connection.
Create the App.config file to define the client endpoint.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="RelayEndpoint" contract="Microsoft.ServiceBus.Samples.IEchoContract" binding="netTcpRelayBinding" bindingConfiguration="default" behaviorConfiguration="sharedSecretEndpointBehavior" address="" /> </client> </system.serviceModel> </configuration>
Similar to configuring a service application, the minimum parameters that you must have to configure an endpoint are the contract that implements the service, and the type of binding used. The address attribute is the Service Bus address. It can be specified explicitly in the configuration file but is usually constructed programmatically using the CreateServiceUri method. This example also associates an endpoint behavior with the client.
Define the endpoint behavior that contains the security settings.
<behaviors> <endpointBehaviors> <behavior name="sharedSecretEndpointBehavior"> <transportClientEndpointBehavior credentialType="SharedSecret"> <clientCredentials> <sharedSecret issuerName="ISSUER_NAME" issuerSecret="ISSUER_SECRET" /> </clientCredentials> </transportClientEndpointBehavior> </behavior> </endpointBehaviors> </behaviors>
In this example, for simplicity the security credentials are defined by using the issuer name and secret in clear text. Note that this is an nonsecure programming practice: a more secure process (and the process used by many of the Service Bus samples) is to query the user for this information. Alternately, you could decide to encrypt the App.config file to avoid exposing this information.
Define the binding that the client application is to use when it connects to Service Bus.
<bindings> <!-- Application Binding --> <netTcpRelayBinding> <!-- Default Binding Configuration--> <binding name="default" /> </binding> </bindings>
You have finished configuring the client application through the App.config file. For more information about creating a Service Bus client application, see Building a Service Bus Client Application.