Share via


My Hello Azure Service Bus WCF Service–Step by step guide

This is the first post in a series to take you through my learning experience to using the Windows Azure service bus. The Service Bus is a concrete implementation of the service bus pattern designed to operate at Internet scope within highly-scalable Microsoft data centers as a constituent of the Windows Azure platform. The Service Bus provides a federated identity and access control mechanism through Access Control, a federated naming system, a dynamic service registry, and a robust messaging fabric capable of overcoming the internet and distributed services connectivity. The internet challenges that any distributed service platform face are summarized in the below diagram.

clip_image002[4]

The way that the service bus overcomes these challenges is by implementing a relay service pattern as below.

clip_image004[4]

Here’s how it works: the on-premises service connects to the relay service through an outbound port and creates a bidirectional socket for communication tied to a particular rendezvous address. The client can then communicate with the on-premises service by sending messages to the relay service targeting the rendezvous address. The relay service will then “relay” messages to the on-premises service through the bidirectional socket already in place. The client does not need a direct connection to the on-premises service nor does it need to know where it resides. The on-premises service doesn’t need any inbound ports open on the firewall. This is how most instant messaging applications work today.

A central component of the Service Bus messaging fabric is a centralized (but highly load-balanced) relay service that supports a variety of different transport protocols and Web services standards, including SOAP, WS-*, and even REST. The relay service provides a variety of different relay connectivity options and can even help negotiate direct peer-to-peer connections when possible.

So in this article I will show you how to develop your first hello service hosted on the service bus. The major steps can be summarized as below.

Step 1: Create a new service bus namespace on the Azure service bus.

Step 2: Implement the service and client.

Step 3: Configure the service and client to use the required relay binding configuration.

So as you can see it is as easy as 1, 2, and 3. Or is it?! J

Step1: Create a new service bus namespace

So you need to go to the URL: https://windows.azure.com/ that will request you either to login using a live ID that has already an active Azure subscription or to sign up. If you already have an account then just logon, or if not you can click sign up.

clip_image006[4]

That would lead you to the purchase screen of subscription options, study your options well and pick whatever type you want or (even better) click on the FREE trial.

clip_image008[4]

Then you will be asked to login using an existing live ID then you will need to go through three steps as below:

clip_image010[4]

clip_image012[4]

Then in the third step you would need to enter a credit card just as a form of verification and voila you have a three month trial subscription on Azure.

Next logon to your management portal using you live ID on the URL: https://windows.azure.com/ it should look something like this.

clip_image014[4]

Now to create a new namespace do the following steps.

· Click on “Service Bus, Access Control & Caching” from the bottom left.

· Then click on “Service Bus”
clip_image016[4]

· Then click “New”
clip_image017[4]

· Enter the namespace name and the portal will check if it is available or not for you and select the needed details as below:
clip_image019[4]

· Click “Create” and the namespace would be ready to use.

· Now we will need three details to get started, the service hosting URL, the secret issuer name and issuer secret. The service hosting URL would be dependent on the namespace you already created so for the shown above example the URL would be: https://MyAzureHello.servicebus.windows.net.

· To get the secret details click on the created namespace.
clip_image021[4]

· Then scroll the right action pan all the way down and click on “View” for the “Default Key”.
clip_image023[4]

· It will then give you the option to copy both the issuer name and the issuer secret to the clipboard. Do this one by one and keep this information were you can use it later.
clip_image024[4]

Step 2: Implement the service and client

This is a straight forward step. Just create two console applications, one for the service host and another for the client and create a service definition as below.

[ServiceContract]

public interface IHelloServiceBus

{

    [OperationContract]

    string SayHello(string name);

}

Listing 1: Service Contract

public class HelloServiceBus : IHelloServiceBus

{

    public string SayHello(string name)

    {

        string greeting = string.Format("Hello {0}!", name);

        Console.WriteLine("Returning: {0}", greeting);

        return greeting;

    }

}

Listing 2: Service Implementation

static void Main(string[] args)

{

    Console.WriteLine("**** Service ****");

    ServiceHost host = new ServiceHost(typeof(HelloServiceBus));

    host.Open();

 

    Console.WriteLine("Press [Enter] to exit");

    Console.ReadLine();

 

    host.Close();

}

Listing 3: Host Implementation

static void Main(string[] args)

{

    Console.WriteLine("**** Client ****");

    Console.WriteLine("Press <Enter> to run client.");

    Console.ReadLine();

    Console.WriteLine("Starting.");

 

    ChannelFactory<IHelloServiceBus> channelFactory =

        new ChannelFactory<IHelloServiceBus>("webRelay");

    IHelloServiceBus channel = channelFactory.CreateChannel();

 

    for (int i = 0; i < 10; i++)

    {

        string response = channel.SayHello("Service Bus");

        Console.WriteLine(response);

    }

 

    channelFactory.Close();

}

Listing 4: Client Implementation

Now we need to configure both the client and service to use the service bus bindings.

Step 3: Configure the service and client to use the required relay binding configuration

All you need to do is to put the proper Azure service bus configuration. But before you do that how would the service and client implementation know where to get the binding implementation from? You did not add any custom references, right?! So here is how.

· Make sure both the client and service is using the .NET 4 profile (not the client profile).

· The go in Visual Studio to tools and then extension manager:
clip_image025[4]

· Click on line gallery and search for “NuGet”
clip_image027

· Click download and hence install the NuGet Package Manager.

· Close the extension manager.

· Right click on the references node for the service project and click “Manage NuGet Packages”.
clip_image028

· Click online.

· Search for “Azure” and select the “Windows Azure Service Bus” and click “Install”.
clip_image030

· Install the same NuGet package to the client project.

· Now that you have the Azure assemblies in place you can change the service and client configurations as below.

<system.serviceModel>

  <services>

    <service name="Service.HelloServiceBus">

      <endpoint address="https://momalek.servicebus.windows.net/helloservicebus" behaviorConfiguration="sharedSecretClientCredentials" binding="ws2007HttpRelayBinding" contract="Service.IHelloServiceBus"/>

    </service>

  </services>

  <behaviors>

    <endpointBehaviors>

      <behavior name="sharedSecretClientCredentials">

        <transportClientEndpointBehavior credentialType="SharedSecret">

          <clientCredentials>

            <sharedSecret issuerName="[Issuer name retrieved before]" issuerSecret="[issuer secret retrieved before]"/>

          </clientCredentials>

        </transportClientEndpointBehavior>

      </behavior>

    </endpointBehaviors>

  </behaviors>

</system.serviceModel>

Listing 5: Service Configuration

<system.serviceModel>

  <client>

    <endpoint address="https://momalek.servicebus.windows.net/helloservicebus" behaviorConfiguration="sharedSecretClientCredentials" binding="ws2007HttpRelayBinding" contract="Service.IHelloServiceBus" name="webRelay"/>

  </client>

  <behaviors>

    <endpointBehaviors>

      <behavior name="sharedSecretClientCredentials">

        <transportClientEndpointBehavior credentialType="SharedSecret">

          <clientCredentials>

            <sharedSecret issuerName="[Issuer name retrieved before]" issuerSecret="[issuer secret retrieved before]"/>

          </clientCredentials>

        </transportClientEndpointBehavior>

      </behavior>

    </endpointBehaviors>

  </behaviors>

</system.serviceModel>

Listing 6: Client Configuration

Now you are ready to start the service and (wait for it to properly start, as it take some time – couple of minutes or so) the client and watch them communicate through the Azure service bus.

Final Notes

During this exercise I tried many bindings and I must say that the most reliable one I used was the “ws2007HttpRelayBinding” (I mean reliable from the perspective of being able to start hosting the service with no problems).

Hosting a service behind a proxy (specially a proxy that requires authentication) is not supported and does not workL. Check this URL: https://msdn.microsoft.com/en-us/library/windowsazure/ee706729.aspx.