How to: Configure a Service Bus Client Using a Configuration File
Updated: March 12, 2015
Once you have defined and implemented a 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 to 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="sbTokenProvider" 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="sbTokenProvider"> <transportClientEndpointBehavior> <tokenProvider> <sharedAccessSignature keyName="SAS POLICY NAME" key="SAS KEY" /> </tokenProvider> </transportClientEndpointBehavior> </behavior> </endpointBehaviors> </behaviors>
In this example, for simplicity the security credentials are defined by using the SAS policy name and key in clear text. Note that this practice can be non-secure: 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.