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/")]

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.

About these ads

26 Responses to “Windows Azure – Service Bus Publish/Subscribe Example”


  1. 1 Vincent-Philippe Lauzon September 1, 2010 at 1:55 pm

    Excellent, I was looking for an example + discussion like this!

    Cheers,

    Vincent-Philippe

  2. 2 Holly November 12, 2012 at 9:27 pm

    Hi there! Someone in my Myspace group shared
    this site with us so I came to look it over.
    I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers!
    Terrific blog and outstanding design.

  3. 3 our website November 22, 2012 at 4:23 pm

    Hi there! Someone in my Facebook group shared this
    website with us so I came to look it over. I’m definitely enjoying the information. I’m
    bookmarking and will be tweeting this to my followers! Exceptional
    blog and wonderful style and design.

  4. 4 Eusebia December 8, 2012 at 5:22 pm

    constantly i used to read smaller articles or reviews
    that as well clear their motive, and that is also happening with this post which I am reading now.

  5. 5 african mango December 10, 2012 at 10:10 am

    Everyone loves it when people get together and share thoughts.

    Great blog, keep it up!

  6. 6 seo plan December 17, 2012 at 1:35 am

    This is an awesome site, I will definitely be sure to add your site
    to my bookmarks!

  7. 7 capitol vs capital January 5, 2013 at 3:30 pm

    Everything is very open with a clear clarification of the issues.
    It was really informative. Your website is very useful.
    Many thanks for sharing!

  8. 8 icomfortreviews.net January 19, 2013 at 7:24 pm

    Pretty section of content. I just stumbled upon your weblog and in
    accession capital to assert that I get in fact enjoyed account your blog posts.

    Anyway I’ll be subscribing to your augment and even I achievement you access consistently rapidly.

  9. 9 My homepage January 24, 2013 at 1:46 pm

    How are you? I came across this site using web search engines.
    I think this info extremely interesting. I’ll make sure to add it to my bookmarks as well as return to find out more. Thank you for the update. I will definitely return.!!!

  10. 10 supplements growth hormone January 28, 2013 at 8:51 am

    Good day! Do you use Twitter? I’d like to follow you if that would be ok. I’m absolutely enjoying your blog and look forward
    to new updates.

  11. 11 Dwain,Whitehead January 30, 2013 at 8:25 am

    I simply wanted to write down a simple comment in order to thank you for
    all of the magnificent recommendations you are showing on this site.
    My time consuming internet look up has at the end been paid with good content to exchange with my companions.
    I would repeat that many of us readers are truly endowed to dwell in a very good network with many brilliant individuals with helpful hints.

    I feel very happy to have come across your website page and look forward to plenty of more fun times reading here.
    Thank you once more for everything.

  12. 12 vodeos porn xxx January 31, 2013 at 11:55 pm

    Hi! I’ve been reading your site for a long time now and finally got the bravery to go ahead and give you a shout out from Humble Tx! Just wanted to say keep up the excellent job!

  13. 13 Jenny February 11, 2013 at 12:27 pm

    What’s up, just wanted to mention, I enjoyed this article. It was practical. Keep on posting!

  14. 14 fitness February 14, 2013 at 3:30 am

    Attractive portion of content. I simply stumbled upon your weblog and
    in accession capital to say that I acquire actually enjoyed
    account your blog posts. Anyway I will be subscribing in
    your augment or even I fulfillment you get admission to consistently rapidly.

  15. 16 http://www.greencalgary.org/member/51681/ March 7, 2013 at 9:48 pm

    Hello There. I discovered your blog the use of msn.
    This is a really neatly written article. I’ll be sure to bookmark it and return to read extra of your useful info. Thank you for the post. I’ll definitely return.

  16. 17 Ramiro March 20, 2013 at 10:07 am

    If some one desires expert view on the topic of running a blog then i propose him/her to pay a visit this website,
    Keep up the pleasant work.

  17. 18 www.cfbr.ie March 26, 2013 at 11:20 am

    I must show thanks to the writer for bailing me out of this dilemma.
    As a result of looking out throughout the search engines and
    getting advice which were not beneficial, I believed my life was over.
    Being alive without the presence of answers to the difficulties
    you’ve resolved through your main article is a crucial case, as well as those which may have badly damaged my entire career if I had not encountered your web blog. That training and kindness in touching all the things was invaluable. I don’t know what I would’ve done if I hadn’t encountered such a subject like this.
    I am able to now look forward to my future. Thanks for your time so much for your specialized and effective guide.
    I will not hesitate to recommend your blog to anyone who needs counselling on this topic.

  18. 19 reklama March 31, 2013 at 10:38 am

    I do not even know how I finished up right here, but I
    assumed this post was once good. I don’t understand who you might be however certainly you’re
    going to a famous blogger if you happen to are not already.
    Cheers!

  19. 20 milf porn videos April 10, 2013 at 3:48 am

    Hi there, I log on to your blogs regularly. Your writing
    style is awesome, keep up the good work!

  20. 21 business planning April 13, 2013 at 10:00 pm

    After looking into a few of the articles on your site,
    I honestly like your way of blogging. I saved it to my bookmark website list and will be checking back in the near future.
    Please visit my website as well and let me
    know your opinion.

  21. 22 pleins de films porno mobile July 10, 2013 at 1:18 am

    You made some really good points there. I checked on the net to find out
    more about the issue and found most people will go along with your views on
    this website.

  22. 23 click here July 15, 2013 at 2:32 am

    I absolutely love your blog and find a lot of your
    post’s to be just what I’m looking for. can you offer guest writers to write content for yourself?
    I wouldn’t mind producing a post or elaborating on some of the subjects you write concerning here. Again, awesome web log!

  23. 24 new mothers August 14, 2013 at 2:42 pm

    Everyone loves it when individuals come together and share thoughts.
    Great blog, keep it up!

  24. 25 http://www.wikipedia.com February 9, 2014 at 8:45 am

    Target Characteristics: These are characteristics that reference the one who receives and operations a message.
    Larger organizations tend to have stricter, expressly written rules on etiquette.
    In addition, Wikipedia can be extremely helpful at
    any time that a student encounters a term, name, and other little bit
    of information that’s unfamiliar.


  1. 1 tyl3r durd@n on cloud computation « Cloudtology Trackback on October 7, 2009 at 3:52 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: