Posts Tagged 'netEventRelayBinding'

Windows Azure – Service Bus Publish/Subscribe Example

Within the Azure Platform, there is a set of services named .NET Services. These set of services were originally known as BizTalk.NET, and it includes the Workflow Services, the Access Control Services, and the one we will talk about, the Service Bus.

servicebus

The Service Bus implements the familiar Enterprise Service Bus Pattern. In a nutshell, the service bus allows for service location unawareness between the service and its consumer, along with a set of other, rather important, capabilities. The Service Bus allows you to build composite applications based on services that you really do not need to know where they are. They could be in servers inside your company, or on a server on the other side of the world; the location is irrelevant. There are, nevertheless, important things you need to know about the service you are calling, namely, security. The Access Control Service integrates seamlessly with the Service Bus to provide authentication and authorization. The Access Control Service will be addressed in some other entry, for now we are concentrating on the Service Bus.

The following diagrams depict different scenarios where it makes sense to use the Service Bus.

scenario1

scenario2

Depending on the Service Bus location, it can take a slightly different designation. If the Service Bus is installed and working on-premises, it is commonly known as an ESB (Enterprise Service Bus), if it is on the cloud, it takes the designation ISB (Internet Service Bus). It is still not clear what Microsoft´s intentions are regarding an on-premises offering of the Azure Platform. The following diagram shows another possible scenario for using the Service Bus.

Scenario 3

As I mentioned before, there are several other benefits associated with the use of the Service Bus that can be leveraged by the configuration shown in this diagram. For instance, the Service Bus also provides protocol mediation allowing use of non-standard bindings inside the enterprise (e.g., NetTcpBinding), and more standard protocols once a request is forwarded to the cloud (e.g., BasicHttpBinding).

Going back to our example, we are going to setup the publisher/subscriber scenario depicted in the following diagram.

Cloud

Let´s start by building the service. To do so follow the steps:

1) Sign in to the Azure Services Platform Portal at http://portal.ex.azure.microsoft.com/

2) Create a solution in the Azure Services Platform Portal. This solution will create an account issued by the Access Control Service (accesscontrol.windows.net). The Access Control Service creates this account for convenience only, and this is going to be deprecated. The Access Control Service is basically an STS (Security Token Service), there is no intention from Microsoft to build yet another Identity Management System. Although it integrates with Identity Management Systems such as Windows CardSpace, Windows Live Id, Active Directory Federation Services, etc.

3) Create a console application named “ESBServiceConsole”

4) Add a reference to the “System.ServiceModel” assembly

5) Add a reference to the “Microsoft.ServiceBus” assembly. You can find this assembly in the folder “C:\Program Files\Microsoft .NET Services SDK (March 2009 CTP)\Assemblies\Microsoft.ServiceBus.dll”. By the way I am using the March 2009 CTP on this example, you can find it at http://www.microsoft.com/downloads/details.aspx?FamilyID=b44c10e8-425c-417f-af10-3d2839a5a362&displaylang=en

6) Add the following interface to the “program.cs” file

 

[ServiceContract(Name = “IEchoContract”, Namespace = http://azure.samples/”)]

public interface IEchoContract

{

[OperationContract(IsOneWay = true)]

void Echo(string text);

}

 

7) Add the following class to the program “program.cs” file

 

[ServiceBehavior(Name = “EchoService”, Namespace = http://azure.samples/”)]

class EchoService : IEchoContract

{

public void Echo(string text)

{

Console.WriteLine(“Echoing: {0}”, text);

}

}

 

8) Add the following code to the “main” function

 

// since we are using a netEventRelayBinding based endpoint we can set the conectivity protocol, in this case we are setting it to http

ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http;

 

// read the solution credentials to connect to the Service Bus. this type of credentials are going to be deprecated, they just exist for convenience, in a real scenario one should use CardSpace, Certificates, Live Services Id, etc.

Console.Write(“Your Solution Name: “);

string solutionName = Console.ReadLine();

Console.Write(“Your Solution Password: “);

string solutionPassword = Console.ReadLine();

 

// create the endpoint address in the solution’s namespace

Uri address = ServiceBusEnvironment.CreateServiceUri(“sb”, solutionName, “EchoService”);

 

// 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();

 

Notice that I chose the Tcp protocol as the connectivity mode. In the client side, I will specify the Http protocol. This is to show that protocol mediation can be accomplished with the use of the Service Bus.

9) Add an “app.config” file to the project

10) Add the following configuration to the “app.config” file

 

<system.serviceModel>

<services>

<service name=ESBServiceConsole.EchoService>

<endpoint contract=ESBServiceConsole.IEchoContract

binding=netEventRelayBinding />

</service>

</services>

</system.serviceModel>

 

11) Compile and run the service. Enter the solution credentials, and you should get the following:

servicerun

Now let´s build a client application.

1) Add a console project named “ESBClientConsole” to the solution.

2) Add a reference to the “System.ServiceModel” assembly.

3) Add a reference to the “Microsoft.ServiceBus” assembly.

4) Add the following interface to the “program.cs” file

 

[ServiceContract(Name = “IEchoContract”, Namespace = http://azure.samples/&#8221;)]

public interface IEchoContract

{

[OperationContract(IsOneWay = true)]

void Echo(string text);

}

 

public interface IEchoChannel : IEchoContract, IClientChannel { }

 

5) Add the following code to the “main” function

 

// since we are using a netEventRelayBinding based endpoint we can set the conectivity protocol, in this case we are setting it to http

ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Tcp;

 

// read the solution credentials to connect to the Service Bus. this type of credentials are going to be deprecated, they just exist for convenience, in a real scenario one should use CardSpace, Certificates, Live Services Id, etc.

Console.Write(“Your Solution Name: “);

string solutionName = Console.ReadLine();

Console.Write(“Your Solution Password: “);

string solutionPassword = Console.ReadLine();

 

// create the service URI based on the solution name

Uri serviceUri = ServiceBusEnvironment.CreateServiceUri(“sb”, solutionName, “EchoService”);

 

// 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

{

channel.Echo(input);

Console.WriteLine(“Done!”);

}

catch (Exception e)

{

Console.WriteLine(“Error: “ + e.Message);

}

input = Console.ReadLine();

}

 

channel.Close();

channelFactory.Close();

 

6) Add an “app.config” file to the project

7) Add the following configuration to the “app.config” file

 

<system.serviceModel>

<client>

<endpoint name=RelayEndpoint

 contract=ESBClientConsole.IEchoContract

binding=netEventRelayBinding/>

</client>

</system.serviceModel>

 

8) Compile the client, run three instances of the service, enter the credentials, run the client and type some text, the result should be as follows.

servicerun2

There you have it, a publish/subscribe example using the Service Bus.

Advertisements