25
Distributed Application Development with NServiceBus David Boike www.make-awesome.com @DavidBoike Presentation and source: http://bit.ly/fOn4U9

Distributed Application Development with NServiceBus

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: Distributed Application Development with NServiceBus

Distributed Application Development with NServiceBus

David Boikewww.make-awesome.com

@DavidBoike

Presentation and source: http://bit.ly/fOn4U9

Page 2: Distributed Application Development with NServiceBus

Real-Life Exampleusing (TransactionScope ts = new TransactionScope()){

InsertOrUpdateAlertPreferences();UpdateAlertEmails();

InternalWebService.UpdateEmailSettings(listID, emailAddresses);

ts.Complete();}

Page 3: Distributed Application Development with NServiceBus

Real-Life Example – It Gets Worseusing (TransactionScope ts = new TransactionScope()){

InsertOrUpdateAlertPreferences();UpdateAlertEmails();

InternalWebService.UpdateEmailSettings(listID, emailAddresses);

ExternalWebService.UpdateSmsSettings(keyword, phoneNumber);

ts.Complete();}

Page 4: Distributed Application Development with NServiceBus

NServiceBus to the rescue!Enterprise Service Bus for .NET

Not a centralized broker like BizTalkNot a services communication framework like

WCFCreated by Udi Dahan, internationally

renowned expert on software architecture and design

Focuses on messaging and publish/subscribeUltra reliableProvides friction against poor architecture

decisions

Page 5: Distributed Application Development with NServiceBus

How it WorksUtilizes assembly scanning for component

discoveryUses marker interfaces to identify message

schemas, message handlers, configuration sources, etc.

Uses dependency injection (inversion of control) to detect and provide dependencies and services

Everything is pluggable

Page 6: Distributed Application Development with NServiceBus

App

How an Endpoint Works

MSMQ

TransactionalReceive

Page 7: Distributed Application Development with NServiceBus

Transaction

App

How an Endpoint Works

MSMQ

TransactionalReceive

Page 8: Distributed Application Development with NServiceBus

Transaction

App

How an Endpoint Works

MSMQ

DB

Perform database operations within the distributed transaction

Page 9: Distributed Application Development with NServiceBus

Transaction

App

How an Endpoint Works

MSMQ

Web Service

Or connect to a traditional web service

Page 10: Distributed Application Development with NServiceBus

Transaction

App

How an Endpoint Works

MSMQ Send or publish additional messages

Page 11: Distributed Application Development with NServiceBus

Transaction

Transient ErrorsIf an exception happens, the transaction rolls back and returns the message to the queue.

TransactionRolling Back

ExceptionApp

MSMQ

Automatic retries are an integral part of the system.

Page 12: Distributed Application Development with NServiceBus

TransactionRolling Back

Poison MessagesAfter a configurable number of retries, poison messages get sent to an error queue.

App

MSMQ

Error

Fix the underlying issue, and you can return error messages to their source queue.

Page 13: Distributed Application Development with NServiceBus

Real-Life Example - RevisitedWebapp Bus.Send(new

SaveAlertsCmd());

AlertService

Bus.Publish(new AlertsUpdatedEvent());

SMS Adapter

Web Svc Adapter

Bus.Subscribe<AlertsUpdatedEvent>();

Subscriptions

Page 14: Distributed Application Development with NServiceBus

Getting StartedGo to http://www.nservicebus.comClick the Download linkFollow the instructionsBe sure to run RunMeFirst.bat from the

command prompt with admin rights!Installs and/or verifies MSMQ configurationSets the appropriate settings on the Microsoft

Distributed Transaction CoordinatorInstalls performance counters

Run from the command prompt so you can see if there are any errors.

Page 15: Distributed Application Development with NServiceBus

Let’s Build Something

Page 16: Distributed Application Development with NServiceBus

In case you missed it…If you are watching the presentation live, the next several slides are apt to be very boring.

Sorry.

For those of you at home, this is what we did so you can follow along in the code samples.

Page 17: Distributed Application Development with NServiceBus

Code Phase 1: MessagingCreate MyMessages assembly

Add reference to NServiceBus.dllAdd CreateUserCmd

Page 18: Distributed Application Development with NServiceBus

Code Phase 1: MessagingCreate UserService assembly

Add reference to NServiceBus.dll, NServiceBus.Core.dll, NServiceBus.Host.exe, and Log4Net

Add IConfigureThisEndpoint, AsA_PublisherAdd UserCreator service implementing

IHandleMessages<CreateUserCmd>Add Start Action to run NServiceBus.Host.exe

Page 19: Distributed Application Development with NServiceBus

Code Phase 1: MessagingModify web project

Add Global.asax, providing a global home for the web application’s IBus instance

Modify Web.config with NServiceBus configuration

Modify Register.aspx.cs to send the message

Page 20: Distributed Application Development with NServiceBus

Code Phase 2: Saga/VerificationCreateUserSagaData : ISagaEntity

Copy properties from CreateUserCmdAdd VerificationString property

Change from service handling CreateUserCmd to saga started by CreateUserCmdSet saga data from commandOverride ConfigureHowToFindSaga, calling

ConfigureMapping<CreateUserCmd>() method

Page 21: Distributed Application Development with NServiceBus

Code Phase 2: Saga/Verification“Send” verification email

Create SendVerificationEmailCmdCreate mock handler that logs verification code

to consoleConfigure UserCreator App.config to send to

the UserService (itself, although this could be a separate project)

Send SendVerificationEmailCmd from the Handle<CreateUserCmd> method

Page 22: Distributed Application Development with NServiceBus

Code Phase 2: Saga/VerificationVerify the email

Create VerifyEmailCmd in MyMessagesCreate UserCreatedEvent in MyMessagesAdd IHandle<VerifyEmailCmd> to saga and

configure how to find saga data from this command

Add implementation, actually creating user in DB

MarkAsComplete(), log, and Bus.Return(0)

Page 23: Distributed Application Development with NServiceBus

Code Phase 2: Saga/VerificationWeb handler for email link

Create Verify.aspxConfigure Web.config to send VerifyEmailCmd

to UserServiceChange Register.aspx.cs redirect from Login to

Verify

Page 24: Distributed Application Development with NServiceBus

All caught up!

Page 25: Distributed Application Development with NServiceBus

Where to learn moreNServiceBus website: www.nservicebus.comMy blog: www.make-awesome.comAsk on Stack Overflow (tag with

“nservicebus”)Udi’s blog: www.udidahan.comAndreas Ohlund’s blog:

www.andreasohlund.netJonathan Oliver’s blog:

www.jonathanoliver.comTwitter: #NServiceBusPresentation Source Code: http://

bit.ly/fOn4U9