in Windows Azure

Programming Microsoft .Net Services

If you are not familiar with Microsoft .Net Services, do read my earlier post

So, are you ready to start programming .Net Services ? Not so fast, there are still certain things that you need to know before you start coding 😉

Microsoft .Net Services Account

First you need a Microsoft .Net Services account to actually host and create services that interact with the Service Bus. You can go to Microsoft Connect and apply for the ‘Azure Services Invitations’. Once you register, you will be put in the application pool and if you are lucky, you might get a token immediately for Microsoft .Net Services.

Once you have the token, proceed here to enter the invitation code. Activate the .Net Services and create a solution. Your solution name/password will be used for authentication when you host your services with Microsoft .Net Services. You can also associate Windows Cardspace or a X.509 Certificate for your solution.

azure-services-credentials

Finally, download the .Net Services SDK here

Connection Modes

Service Bus supports three types of connection modes:

Relayed

image

1) Uses relay between server and client.

2) This is the default connection mode

Direct

image

1) Direct connection between server and client, though a relayed connection is used for initialization

2) If Direct connection is not possible, then relay will be used

Hybrid

image

1) Combination of Relay and Direct

2) Establishes a relayed connection, but uses direct connection whenever possible

Service Bus Bindings

The Service Bus is entirely built on Windows Communication Foundation (WCF) . If you are not familiar with WCF, then you could start here. To build a simple ‘Hello World’ service, all you need to do is to create a WCF Service and WCF Client with a ‘specific’ service bus binding. The bindings currently available are:

  • WebHttpRelayBinding
  • BasicHttpRelayBinding
  • BasicHttpRelayContextBinding
  • WSHttpRelayBinding
  • WS2007HttpRelayBinding
  • WS2007FederationHttpBinding
  • NetTcpRelayBinding
  • NetOnewayRelayBinding
  • NetEventRelayBinding

‘Hello World’

Basic steps to write a ‘Hello World’ sample are:

1) Create a WCF Service (Service Contract)

2) Create a Service Bus binding

3) Host in the Service Bus (by authenticating yourself)

4) Write a basic client

5) The client also needs to authenticate with the Service Bus to interact with your service

Building the ‘Hello World’ Service

1) Create your Service Contract

[ServiceContract]public interface IHelloWorldService{    [OperationContract]    string HelloWorld();}

 

2) Implement your Service

[ServiceBehavior(Name = "HelloWorldService",     Namespace = "http://Chaks.Samples.MyService/HelloWorld/")]public class HelloWorldService : IHelloWorldService{    #region IMyService Members    public string HelloWorld()    {        return "[Service returns] Hello World";    }    #endregion}

3) Edit your app.config to hold the WCF endpoints

<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <services>      <service name="Chaks.Samples.MyService.HelloWorldService">        <endpoint name="HelloWorldRelayEndpoint"                  contract="Chaks.Samples.MyService.IHelloWorldService"                  binding="netTcpRelayBinding"/>      </service>    </services>  </system.serviceModel></configuration>

The binding we are using here netTcpRelayBinding and the connection mode is the default, which is Relay.

4) You can now host your service. To do so,

a. Enter your solution name and password

Console.WriteLine("Enter your solution name : ");string solutionName = Console.ReadLine();Console.WriteLine("Enter your solution password : ");string solutionPassword = ReadPassword();

b. Create the Service Bus URI

string serviceUri = String.Format("sb://{0}/services/{1}/HelloWorldService/",                             ServiceBusEnvironment.DefaultRelayHostName,                             solutionName);

c. Specify the Transport Credentials

TransportClientEndpointBehavior endpointBehavior = new TransportClientEndpointBehavior();endpointBehavior.CredentialType = TransportClientCredentialType.UserNamePassword;endpointBehavior.Credentials.UserName.UserName = solutionName;endpointBehavior.Credentials.UserName.Password = solutionPassword;

d. Create your Service Host

ServiceHost serviceHost = new ServiceHost(typeof(HelloWorldService), new Uri(serviceUri));

e. Apply the credentials to the endpoints

foreach (ServiceEndpoint endpoint in serviceHost.Description.Endpoints){    endpoint.Behaviors.Add(endpointBehavior);}

f. Open the Service

serviceHost.Open();

If you have entered the proper credentials, your service will now be hosted at the Service Bus – in the Cloud!

Building the ‘Hello World’ Client

1) Enter your solution name and password

Console.WriteLine("Enter your solution name : ");string solutionName = Console.ReadLine();Console.WriteLine("Enter your solution password : ");string solutionPassword = ReadPassword();

2) Create the Service Bus URI

string serviceUri = String.Format("sb://{0}/services/{1}/HelloWorldService/",                             ServiceBusEnvironment.DefaultRelayHostName,                             solutionName);

3) Specify the Transport Credentials

TransportClientEndpointBehavior endpointBehavior = new TransportClientEndpointBehavior();endpointBehavior.CredentialType = TransportClientCredentialType.UserNamePassword;endpointBehavior.Credentials.UserName.UserName = solutionName;endpointBehavior.Credentials.UserName.Password = solutionPassword;

4) Create our HelloWorld channel

public interface IHelloWorldChannel : IHelloWorldService, IClientChannel { }

5) Create the ChannelFactory

ChannelFactory<IHelloWorldChannel> channelFactory =    new ChannelFactory<IHelloWorldChannel>("HelloWorldRelayEndpoint",                 new EndpointAddress(new Uri(serviceUri)));

6) Apply the credentials to the endpoint and create the service channel

channelFactory.Endpoint.Behaviors.Add(endpointBehavior);IHelloWorldChannel channel = channelFactory.CreateChannel();

7) Open the channel and invoke the HelloWorld() method

channel.Open();Console.WriteLine(channel.HelloWorld());channel.Close();

If everything worked fine, you should get a reply from the Service.

Below is the screenshot of the output from our ‘Hello World’ Service and Client

hello-world-output

You can download the sample below

 

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.

  1. Hi,

    I am trying such sample using WCF project template, so that I can add the service reference in client project, using Add Service Referece feature.

    But I am getting error while I ruin the client, "Transport security is required for this connection.The endpoint requires a secure channel to connect."

    can you help in some way ?

    ~Thanks,

    Prashant