Just got an invitation code to play with the Azure stock - note that the activation code is per solution and you need a password

Creating a solution in Azure using .NET Services and SQL Services

 Azure Services Create Solution

Remember to save the password for the solution. Password xxxxxx (save this in a notepad)

STEP 2 – Create your service.

 

These are the type of services that you are available..

 

Then you open your cloud solution it will look like this:

To run your first Internet Service Bus Service run the Echo Solution in the November CTP

Edition of .NET services add on for Visual Studio 2008 – compile and build both service and client projects and it will then ask you for your .NET Service Solution name and password. Once you enter these two credentials you have your first service running on the world. Okay this one just echo's anything you type into the client and nothing else – the interesting part is in the service configure file not the code. Notice the endpoint of my service…

 

Once you enter the solution name (must match the name of the solution name activated) and password, now you can run your client as well - screen shots below

 image6

Overview of the Echo solution in the .NET Services for Azure SDK.

This sample demonstrates how to use the Microsoft .NET Services Service Bus and the NetTcpRelayBinding. It's highly recommended that you take a look at the Technical Overview and the Service Bus Reference document to get yourself oriented on the Service Bus before diving into the samples.

This sample shows a simple service and client that communicate through the Service Bus. When the service application is started, it asks for your .NET Services Solution Credentials and opens an endpoint on the Service Bus. Once opened, this endpoint has a well-known URI on the Service Bus and is, irrespective of whether your machine is residing behind a Firewall or Network Address Translation, reachable from anywhere.

Clients accessing the endpoints must have permissions to talk to the endpoint. The client application therefore also asks for your Solution credentials, authenticates with the Access Control service, and acquires an access token that proves to the Service Bus infrastructure that the client is authorized to access the endpoint. Once the client is connected, you can type messages into the Client application which will be echoed back by the Service.

Like with all other samples in the Service Bus section of the .NET Service SDK you will likely only realize the significant difference between a normal Windows Communication Foundation service and a Service Bus service by running the Client and Service applications on different machines and, preferably, even on disjoint networks.

Prerequisites

If you haven't already done so, please read the common prerequisites document that explains how to sign up for a Microsoft .NET Services account and how to configure your environment. It also contains important information about the default security settings for your environment that you need to be aware of.

Echo Service

The service implements a  very simple contract with a single operation named 'Echo'. The 'Echo' service doesn't do anything interesting from a functional perspective; it accepts a string and echoes the string back.

[ServiceBehavior(Name = "EchoService", Namespace = "http://samples.Microsoft.com/ServiceModel/Relay/")]
class EchoService : IEchoContract
{
    public string Echo(string text)
    {
        Console.WriteLine("Echoing: {0}", text);
        return text;
    }
} 

More interesting than the actual service implementation are the service's configuration (in App.config) and the hosting code for the service. The service configuration is shown below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <!-- Application Service -->
      <service name="Microsoft.ServiceBus.Samples.EchoService">
        <endpoint contract="Microsoft.ServiceBus.Samples.IEchoContract"
      binding="netTcpRelayBinding" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

Assuming you have some prior knowledge of the structure of configuration files for the Windows Communication Foundation (Configuration Overview, Reference), you will find that the configuration file is just like any other WCF configuration file with the exception that configured service endpoint refers a "netTcpRelayBinding", which isn't part of the .NET Framework.

The NetTcpRelayBinding is one of the new bindings introduced with the Service Bus and the respective configuration element is added to the global configuration schema when the client runtime components are installed; at installation time, the respective configuration extensions are registered in the machine.config configuration file in the .NET Framework runtime directory.

Looking at the code hosting the service (in Program.cs), there are two key aspects of interest:

  • How does addressing work?
  • How does authentication and authorization work?
Console.Write("Your Solution Name: ");
string solutionName = Console.ReadLine();
Console.Write("Your Solution Password: ");
string solutionPassword = ReadPassword();

// create the endpoint address in the solution's namespace
Uri address = new Uri(String.Format("sb://{0}/services/{1}/EchoService/",
                                    ServiceBusEnvironment.DefaultRelayHostName,
                                    solutionName));

// create the credentials object for the endpoint
TransportClientEndpointBehavior userNamePasswordServiceBusCredential = new TransportClientEndpointBehavior();
userNamePasswordServiceBusCredential.CredentialType = TransportClientCredentialType.UserNamePassword;
userNamePasswordServiceBusCredential.Credentials.UserName.UserName = solutionName;
userNamePasswordServiceBusCredential.Credentials.UserName.Password = solutionPassword;

// create the service host reading the configuration
ServiceHost host = new ServiceHost(typeof(EchoService), address);

// add the Service Bus credentials to all endpoints specified in configuration
foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
{
    endpoint.Behaviors.Add(userNamePasswordServiceBusCredential);
}

// open the service
host.Open();

Console.WriteLine("Service address: " + address);
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();

// close the service
host.Close();

In order to hook a listening service into the Service Bus, the Service Bus service needs to be able to verify that the owner of the listening service is authorized to do so.

Authentication and authorization are both performed by the Microsoft .NET Services Access Control Service. In order to make these steps simple, the Microsoft.ServiceBus Framework contains a set of transport client credential helpers that automatically deal acquiring required security tokens.

The credential used in this example is a simple Username/Password credential that is backed by the Solution credentials you set up when signing up for Microsoft .NET Services. To associate a listener endpoint with its Service Bus credentials, a TransportClientEndpointBehavior must be added to the respective endpoint's behavior collection. If your service were to expose multiple endpoints through the Relay, the same behavior instance can be added to all those endpoints.

Each Microsoft .NET Services solution automatically owns a branch of the Service Bus global namespace. "Your" Solution namespace branch is rooted at

[scheme]://servicebus.windows.net/services/solution-name/

whereby [scheme] is either "sb" (as in this example) or "http" or "https". The namespace owner can subdivide that namespace and organize services onto that namespace as needed and define rules in the Access Control Service to guard access to branches of the namespace.

The code above prompts for the Solution credential and then constructs the listening URI using that information. The static ServiceBusEnvironment.DefaultRelayHostName property currently yields the string "servicebus.windows.net", but it is strongly recommended to use the property and not the literal string value as the domain name may change in future releases. At present, the resulting listener URI is "sb://servicebus.windows.net/services/solution-name/EchoService/". These URIs can be used as an absolute listening URI for endpoints or as the service's base address.

Echo Service Client

The client application mirrors the service in terms of its configuration.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <client>
      <!-- Application Endpoint -->
      <endpoint name="RelayEndpoint"
                contract="Microsoft.ServiceBus.Samples.IEchoContract"
                binding="netTcpRelayBinding"/>
    </client>
  </system.serviceModel>
</configuration>

When started, it asks for the Solution credentials, creates a channel to the Service and sends requests by calling  the 'Echo' operation. Once the interaction is complete, the client closes the channel and exits.

Console.Write("Your Solution Name: ");
string solutionName = Console.ReadLine();
Console.Write("Your Solution Password: ");
string solutionPassword = ReadPassword();

// create the service URI based on the solution name
Uri serviceUri = new Uri(String.Format("sb://{0}/services/{1}/EchoService/",
                                       ServiceBusEnvironment.DefaultRelayHostName,
                                       solutionName));

// create the credentials object for the endpoint
TransportClientEndpointBehavior userNamePasswordServiceBusCredential = new TransportClientEndpointBehavior();
userNamePasswordServiceBusCredential.CredentialType = TransportClientCredentialType.UserNamePassword;
userNamePasswordServiceBusCredential.Credentials.UserName.UserName = solutionName;
userNamePasswordServiceBusCredential.Credentials.UserName.Password = solutionPassword;

// create the channel factory loading the configuration
ChannelFactory<IEchoChannel> channelFactory = 
     new ChannelFactory<IEchoChannel>("RelayEndpoint", new EndpointAddress(serviceUri));

// apply the Service Bus credentials
channelFactory.Endpoint.Behaviors.Add(userNamePasswordServiceBusCredential);

// create and open the client channel
IEchoChannel channel = channelFactory.CreateChannel();
channel.Open();


Console.WriteLine("Enter text to echo (or [Enter] to exit):");
string input = Console.ReadLine();
while (input != String.Empty)
{
    try
    {
        Console.WriteLine("Server echoed: {0}", channel.Echo(input));
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: " + e.Message);
    }
    input = Console.ReadLine();
}

channel.Close();
channelFactory.Close();

Running the Sample

To run the sample, build the solution in Visual Studio or from the command line and run the two resulting executables. The 'service' should obviously be started first and the 'client' second. For either program you will be prompted for your Solution credentials.

Once the service and the client have been opened, you can start typing messages into the client application which will be echoed back by the service.

Expected Output – Client
Your Solution Name: solution-name 
Your Solution Password: ******
Enter text to echo (or [Enter] to exit): Hello, World!
Server echoed: Hello, World!
Expected Output – Service
Your Solution Name: solution-name 
Your Solution Password: ******
Service address: sb://servicebus.windows.net/services/solution-name/EchoService/
Press [Enter] to exit
Echoing: Hello, World!