Distributed Application Development with NServiceBus


Published on

Building reliable, scalable, maintainable distributed applications is next to impossible without the right tools. NServiceBus is an open-source .NET framework that will help you to do just that.

This code-focused presentation covers the fundamentals of NServiceBus development, including one-way messaging, publish and subscribe, and implementing long-running business processes.

In the code example, we start with fairly simple code for a user to create an account on a website and expand it to include an email verification feature using NServiceBus.

In the first phase, we’ll move the work of creating the user into the service layer, so that we can get an example of how NServiceBus works at its most fundamental level.

In the second phase, we will up the ante significantly, using a Saga (a long running business process) to implement an email verification check. This includes sending an email with a verification code to the user, and then verifying that code before finally creating the user.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Distributed Application Development with NServiceBus

  1. 1. Distributed Application Development with NServiceBus<br />David Boike<br />www.make-awesome.com<br />@DavidBoike<br />Presentation and source: http://bit.ly/fOn4U9<br />
  2. 2. Real Life Example<br />using (TransactionScopets = newTransactionScope())<br />{<br />InsertOrUpdateAlertPreferences();<br />UpdateAlertEmails();<br />InternalWebService.UpdateEmailSettings(listID, emailAddresses);<br />ts.Complete();<br />}<br />
  3. 3. Real Life Example – It Gets Worse<br />using (TransactionScopets = newTransactionScope())<br />{<br />InsertOrUpdateAlertPreferences();<br />UpdateAlertEmails();<br />InternalWebService.UpdateEmailSettings(listID, emailAddresses);<br />ExternalWebService.UpdateSmsSettings(keyword, phoneNumber);<br />ts.Complete();<br />}<br />
  4. 4. NServiceBus to the rescue!<br />Enterprise Service Bus for .NET<br />Not a centralized broker like BizTalk<br />Not a services communication framework like WCF<br />Created UdiDahan, internationally renowned expert on software architecture and design<br />Focuses on messaging and publish/subscribe<br />Ultra reliable<br />Provides friction against poor architecture decisions<br />
  5. 5. How it Works<br />Utilizes assembly scanning for component discovery<br />Uses marker interfaces to identify message schemas, message handlers, configuration sources, etc.<br />Uses dependency injection (inversion of control) to detect and provide dependencies and services<br />Everything is pluggable<br />
  6. 6. App<br />How an Endpoint Works<br />MSMQ<br />Transactional<br />Receive<br />
  7. 7. Transaction<br />App<br />How an Endpoint Works<br />MSMQ<br />Transactional<br />Receive<br />
  8. 8. Transaction<br />How an Endpoint Works<br />Perform database operations within the distributed transaction<br />MSMQ<br />DB<br />App<br />
  9. 9. Transaction<br />How an Endpoint Works<br />Or connect to a traditional web service<br />MSMQ<br />App<br />Web Service<br />
  10. 10. Transaction<br />App<br />How an Endpoint Works<br />Send or publish additional messages<br />MSMQ<br />
  11. 11. Transaction<br />Transient Errors<br />If an exception happens, the transaction rolls back, returning the message to the queue.<br />Transaction<br />Rolling Back<br />MSMQ<br />Automatic retries are an integral part of the system.<br />Exception<br />App<br />
  12. 12. Poison Messages<br />After a configurable number of retries, poison messages get sent to an error queue.<br />Transaction<br />Rolling Back<br />MSMQ<br />Fix the underlying issue, and you can return error messages to their source queue.<br />App<br />Error<br />
  13. 13. Real Life Example - Revisited<br />Webapp<br />Bus.Send(new SaveAlertsCmd());<br />Alert<br />Service<br />Bus.Publish(new AlertsUpdatedEvent());<br />Subscriptions<br />Bus.Subscribe<AlertsUpdatedEvent>();<br />Web Svc Adapter<br />SMS Adapter<br />
  14. 14. Getting Started<br />Go to http://www.nservicebus.com<br />Click the Download link<br />Follow the instructions<br />Be sure to run RunMeFirst.bat via the command prompt with admin rights!<br />Installs and/or verifies MSMQ configuration<br />Sets the appropriate settings on the Microsoft Distributed Transaction Coordinator<br />Installs performance counters<br />Run from the command prompt so you can see if there are any errors.<br />
  15. 15. Let’s Build Something<br />
  16. 16. In case you missed it…<br />If you are watching the presentation live, the next several slides are apt to be very boring.<br />Sorry.<br />For those of you at home, this is what we did so you can follow along in the code samples.<br />
  17. 17. Code Phase 1: Messaging<br />Create MyMessages assembly<br />Add reference to NServiceBus.dll<br />Add CreateUserCmd<br />
  18. 18. Code Phase 1: Messaging<br />Create UserService assembly<br />Add reference to NServiceBus.dll, NServiceBus.Core.dll, NServiceBus.Host.exe, and Log4Net<br />Add IConfigureThisEndpoint, AsA_Publisher<br />Add UserCreatorservice implementing IHandleMessages<CreateUserCmd><br />Add Start Action to run NServiceBus.Host.exe<br />
  19. 19. Modify web project<br />Add Global.asax, providing a global home for the web application’s IBus instance<br />Modify Web.config with NServiceBus configuration<br />Modify Register.aspx.cs to send the message<br />
  20. 20. Code phase 2: Saga/Verification<br />CreateUserSagaData : ISagaEntity<br />Copy properties from CreateUserCmd<br />Add VerificationString property<br />Change from service handling CreateUserCmd to saga started by CreateUserCmd<br />Set saga data from command<br />Override ConfigureHowToFindSaga, calling ConfigureMapping<CreateUserCmd>() method<br />
  21. 21. Code phase 2: Saga/Verification<br />“Send” verification email<br />Create SendVerificationEmailCmd<br />Create mock handler that logs verification code to console<br />Configure UserCreatorApp.config to send to the UserService (itself, although this could be a separate project)<br />Send SendVerificationEmailCmd from the Handle<CreateUserCmd> method<br />
  22. 22. Code phase 2: Saga/Verification<br />Verify the Email<br />Create VerifyEmailCmd in MyMessages<br />Create UserCreatedEvent in MyMessages<br />Add IHandle<VerifyEmailCmd> to saga and configure how to find saga data from this command<br />Add implementation, actually creating user in DB<br />MarkAsComplete(), log, and Bus.Return(0)<br />
  23. 23. Code phase 2: Saga/Verification<br />Web Handler for email link<br />Create Verify.aspx<br />Configure Web.config to send VerifyEmailCmd to UserService<br />Change Register.aspx.cs redirect from Login to Verify<br />
  24. 24. All caught up!<br />
  25. 25. Where to learn more<br />NServiceBus Website: www.nservicebus.com<br />My Blog: www.make-awesome.com/tags/nservicebus<br />Ask on Stack Overflow (tag with “nservicebus”)<br />Udi’s Blog: www.udidahan.com<br />Andreas Ohlund’s Blog: www.andreasohlund.net<br />Jonathan Oliver’s Blog: www.jonathanoliver.com<br />Twitter: #NServiceBus<br />