Home > dotnet 4, NService Bus > Getting started with NServiceBus

Getting started with NServiceBus

In this post I am going to give a quick introduction into NServiceBus and how this can change the way you think or design distributed application for an enterprise.

Before we get into specifics of NServiceBus lets see what is an Enterprise Service bus and how does it fits into the realm of an enterprise. So the Wikipedia definition is

   An enterprise service bus (ESB) is a software architecture model used for designing and    implementing the interaction and communication between mutually interacting software    applications in Service Oriented Architecture. As a software architecture model for    distributed computing it is a specialty variant of the more general client-server software    architecture model and promotes strictly asynchronous message oriented design for    communication and interaction between applications. Its primary use is in Enterprise    Application Integration of heterogeneous and complex landscapes.

So here are some of the key features of NServiceBus :-

  • It’s not a centralized broker like BizTalk or IBM Websphere Message Broker
  • It’s not a Service Communication Framework like WCF.
  • Build on top of MSMQ.
  • Focuses on messaging and publish/subscribe model.
  • It’s very robust and reliable
  • There is no synchronous communication
  • The Community Edition of NServiceBus is absolutely free and can handle up to 30 messages/sec which comes out to be around 2.59 million message a day on a decent quad-core server machine.
  • The licensed version of NService is merely $2000 per license and can handle 100 messages/sec or 86.4 million messages.
  • Highly extensible
  • Written by the software simplest the great Udi Dahan.

Okay that’s was like the product overview but if you are like me, you might be itching to go ahead and use this stuff. So here is how you can jump into the NServiceBus wagon (oops the pun ).

  • Go to http://nservicebus.com
  • Click on “Download v2.5 SP1”
  • Unzip the file and run RunMeFirst.bat as an admin
  • Install or configure MSMQ
  • Go to the samples folder and open the Full Duplex project

Just to get a feel of what the project structure looks so here it.

Full Duplex Project Structure

As you can see the project structure is self-explanatory, there is a client and a server and client is going to send a message and the server will take process the message and return it back to client.Lets run the project and see what happens.

Full Duplex Console Output

and as you can see in the above figure that client and server are launched and ready to exchange messages. Lets hit enter in the Client console window and you will see the message gets send to the server with a Guid, which is received by the server and server sends back the message back to the client.

Full Duplex message send and received

So till now nothing special but it’s time to go deeper and start tinkering with NServiceBus to understand some more. What I’ll now do is from the solution this time I’ll run only the client and not the server to mimic “a real life distributed application with reliable messaging” kind of scenario. So lets run the client project in isolation.

Running the client in isolation

and we see that the client console windows gets launched and let hit enter to send a message to the service.So this is how it looks when we do that.

Full Duplex Sending message from client

Now we will look how this message gets routed by NServiceBus. Lets open MSMQ and see where this message is routed and stored in MSMQ.

msmq client and server message queues

If you remember from the client console window that a message with a guid 3e8eeb98-0130-429a-862c-a56f5a02d95b but the message id in the server queue is something different and you must be wondering about this mismatch.

Lets open the message and see what’s in there.

MSMQ message body

So here in the message you can see the Data Id which matches the guid and this is how the server can correlated the incoming messaging and outgoing message to the client.

Now lets run the solution again and see how the NServiceBus will pick up the message process it and send it back to client and client will display the returned message from the server.

Full Duplex - Offline processing

Viola !!! We see the NServiceBus picks up the message and processes it and return the message back to client and the client receives the processed message.If at this point you open the MSMQ and you will see that the message has been processed from the Server Queue and since the client has received the message the message disappears from the client queue. This is a good project to understand the core of NServiceBus and getting started with it.

One of thing I like about NServiceBus is the documentation is superb and all the sample projects are really good to understand. Hopefully I will able to bring more real-life & complex implementation of NServiceBus as I go my implementing in my current project.Till then …..

Enjoy !!!

Advertisements
  1. Jeremy Jenkins
    October 17, 2011 at 5:56 pm

    hi there thanks for the information you provided. i’m wondering for a real world scenario. i basically need and NServiceBus hosted service that I will subscribe to. I want to send a custom object and receive a custom object response instead of an error code enum. Can i do this?:

    • October 20, 2011 at 5:50 am

      Hi Jeremy, yes you can pass a custom object back as a response anything which can be serialized can be send.However, NServiceBus calls are asynchronous and if the service passes the message back to the client but the client is not available then you won’t be able to get that object back. If you planning on passing an state of an object ( domain object ) then you are better off looking into more domain driven events like pattern such as CQRS .

      I hope my suggestion has put you into the right direction 🙂

  2. March 15, 2014 at 3:39 pm

    This is very interesting, You’re a very skilled blogger.
    I have joined your feed and look forward to seeking more of your magnificent post.

    Also, I have shared your site in my social networks!

    • April 1, 2014 at 4:14 am

      Thanks mate for your compliment and sharing my website, I hope I will continue to post more interesting and useful blogs in the future.

  3. Swapnil Bhosale
    July 6, 2016 at 6:53 am

    Thanks for the article. I understood how the messages are getting exchanged between client and server. But this is what MSMQ service does. I will like to know what advantage it gave you by implementing NSB.

  4. MIK
    September 7, 2016 at 1:36 pm

    Hi- Just read that the NServiceBus no longer has a free/community edition. Please check the url -http://particular.net/licenseagreement .
    My company would not invest money for license.
    Please advise what are my options.
    Thanks

  1. March 31, 2012 at 10:22 pm
  2. August 21, 2013 at 12:45 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

%d bloggers like this: