Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

How to: Configure a Service Bus Service Programmatically

Once you have defined and implemented the interface for the Service Bus service in your code, you can start configuring your application. Note that configuring an Service Bus application is very similar to configuring a Windows Communication Foundation (WCF) application, as described in Configuring Services in the WCF documentation. Therefore, this topic contains a simplified procedure for configuring an Service Bus application, and also a discussion of the setting specific to the Service Bus.

In addition to the issues discussed in Configuring a WCF Service to Register with the Service Bus, an Service Bus service must determine what kind of authentication and transport security, if any, is required. Authentication security is the type of security necessary for the service to connect to the Service Bus. A service is always required to present authentication credentials to the Service Bus, usually in the form of a shared secret (that is, issuer name and secret) token. However, the service also determines what type of authentication credentials the client applications must use in order to connect to the service. By default, client authentication is set to RelayClientAuthenticationType.RelayAccessToken, which means that the client must present some form of authentication to the Service Bus. In the current version of Windows Azure, this is always another shared secret token. In contrast, transport security determines whether it must connect with some form of secure line. This is referred to as “end-to-end” security because it covers the whole connection between the service, the Service Bus, and the client. In contrast, client authentication covers only the required relationship to connect from the service to the Service Bus. By default, the transport security is set to EndToEndSecurityMode.Transport. This means that security is provided using some form of secure transport, such as HTTPS. It is recommended that you keep the end-to-end security mode set to Transport unless you have a compelling reason to change it, as doing this might reduce the security of your application. For more information about security settings, see Securing and Authenticating a Service Bus Connection

The following procedure describes how to configure an Service Bus service programmatically.

To programmatically configure a Service Bus Service

  1. Create the URI of the endpoint that includes your service namespace name and schema type.

    string serviceNamespace = "myServiceNamespace";
    Uri uri = ServiceBusEnvironemnt.CreateServiceUri("sb", serviceNamespace, "sample/log/";
    
    

    The prefix “sb” indicates that this URI uses the Service Bus schema. Other schemas include HTTP or HTTPS.

  2. Instantiate the host with the contract and URI.

    host = new ServiceHost(typeof(EchoService), uri);
    
  3. Declare and implement the type of authentication credentials to use.

    string issuerName = "MY ISSUER NAME"
    string issuerSecret = "MY SECRET";
    TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
    sharedSecretServiceBusCredential.CredentialType = TransportClientCredentialType.SharedSecret;
    sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = issuerName;
    sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = issuerSecret;
    

    All services are required to use authentication credentials to connect to the Service Bus. Note that hard-coding the issuer name and secret into your code is not a secure practice. For example, many of the samples in the Windows Azure SDK prompt the user for this information.

  4. Declare the type and instance of the contract.

    ContractDescription contractDescription = ContractDescription.GetContract(typeof(IEchoContract), typeof(EchoService));
    
  5. Add the contract description to the service endpoint.

    ServiceEndpoint serviceEndPoint = new ServiceEndpoint(contractDescription);
    
  6. Add the URI to the service endpoint.

    serviceEndPoint.Address = new EndpointAddress(uri);
    
  7. Declare the type of binding to use for the endpoint.

    serviceEndPoint.Binding = new NetTcpRelayBinding();
    

    At this point you can declare the Authentication and EndToEndSecurity mode. This particular example uses the default constructor, which sets the EndToEndSecurityMode to Transport and the RelayClientAuthenticationType to RelayAccessToken. Therefore,, the following snippet is identical to the default constructor, except that it sets those two parameters explicitly:

    serviceEndPoint.Binding = new NetTcpRelayBinding(EndToEndSecurityMode.Transport, RelayClientAuthenticationType.RelayAccessToken);
    
  8. Add the security credentials to the endpoint.

    serviceEndpoint.Behaviors.Add(sharedSecretServiceBusCredential);
    

    These security credentials are required for all services to authenticate with the Service Bus. Because we set the RelayClientAuthenticationType to RelayAccessToken (either by default or explicitly), any client applications are also required to use the same type of authentication credentials.

  9. Add the endpoint to the host.

    host.Description.Endpoints.Add(serviceEndPoint);
    

    You have now created the minimum configuration necessary for an Service Bus service application. At this point, you can add more service-level or endpoint-level configurations, as you would with any other WCF application. For more information about configuring a WCF application, see Configuring Services in the WCF documentation. When you are finished configuring your application, you can host and run your application. For more information, see Building a Service for the Service Bus.

Example

The following example shows how to define configuration information programmatically. The main difference is that all information is set programmatically; in the tutorial, some information not specific to Windows Azure is stored in an App.config file.


namespace AzureSample_WorkerRole
{
    public class WorkerRole : RoleEntryPoint
    {
        private ServiceHost host;

        public override void Start()
        {
            string serviceNamespace = "myDomainName";
            string issuerName = "MY ISSUER NAME"
            string issuerSecret = "MY SECRET";

            Uri uri = ServiceBusEnvironemnt.CreateServiceUri("sb", serviceNamespace, "sample/log/";
            
            host = new ServiceHost(typeof(EchoService), uri);

            TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
            sharedSecretServiceBusCredential.CredentialType = TransportClientCredentialType.SharedSecret;
            sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = issuerName;
            sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = issuerSecret;

            ContractDescription contractDescription = ContractDescription.GetContract(typeof(IEchoContract), typeof(EchoService));

            ServiceEndpoint serviceEndPoint = new ServiceEndpoint(contractDescription);
            serviceEndPoint.Address = new EndpointAddress(uri);
            
            serviceEndPoint.Binding = new NetTcpRelayBinding();
            
           serviceEndpoint.Behaviors.Add(sharedSecretServiceBusCredential);
            host.Description.Endpoints.Add(serviceEndPoint);

            host.Open();

            while (true)
            {
                //Loop
            }
        }

        public override void Stop()
        {
            host.Close();
            base.Stop();
        }

        public override RoleStatus GetHealthStatus()
        {
            // This is a sample worker implementation. Replace with your logic.
            return RoleStatus.Healthy;
        }
    }
}

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.