SlideShare a Scribd company logo
NSERVICEBUS

Inspired by: The course authored by Udi Dahan




                 Oslo/Fagdag
          Espen Ekvang/Tomas Jansson
                  01/03/2013
AGENDA                   2




• Intro
• Messaging and queues
• Testing
• SOA
• Saga
INTRO                                  3




• Fallacies of distributed computing
• Why NServiceBus?
• Bus vs. Broker
• Service orientation
• Excercises
FALLACIES OF DISTRIBUTED COMPUTING                       4




1.   The network is reliable
2.   Latency isn’t a problem
3.   Bandwidth isn’t a problem
4.   The network is secure
5.   The topology won’t change
6.   The administrator will know what to do
7.   Transport cost isn’t a problem
8.   The network is homogeneous




            Cant’ assume WHEN the message will arrive,
                                  IF AT ALL
WHY NSERVICEBUS                                                                     5




1.   The network is reliable
2.   Latency isn’t a problem            NServiceBus addresses the first five directly
3.   Bandwidth isn’t a problem
4.   The network is secure
5.   The topology won’t change
6.   The administrator will know what to do
7.   Transport cost isn’t a problem
8.   The network is homogeneous




The most developer-friendly service bus for SOA on .NET
BUS VS. BROKER                                                         6



•    Bus is not necessarily physically separate
•    Simpler; no routing or service fail over
•    No single point of failure
                                                        App

                App
               Buss.dll




                                                        Broker




     App                   App
    Buss.dll              Buss.dll                App            App
TENETS OF SERVICE ORIENTATION                    7



•   Services are autonomous
•   Share contract & schema, not class or type
•   Boundaries are explicit
•   Compatibitility is base on Policy
LAYERS & COUPLING                                    8




                                  Tight coupling
                                 Loose coupling

                    Sales    Shipping      CRM
   UI
   BL
   DAL

     DB                     Referential Integrity
                             Reintroduces coupling
WHEN CAN I WRITE SOME CODE?                                               9




• Getting started
  •   New class library
  •   Install-Package NServiceBus.Host
• Logging
  •   NServiceBus uses log4net, you can configure logging in app.config
  •   Default output is console
EXERCISES     10




HELLO WORLD


LOGGING
MESSAGING AND QUEUES           11




• Store & forward
• Dangers of store & forward
• Request/Response
• Messaging and NServiceBus
• Exercises
STORE & FORWARD                                                12



                                SERVER




                                MSMQ
         OUTGOING                                   INCOMING




                    Store & Forward writes to disk
                    Resilient in the face of failures




                                MSMQ
         OUTGOING                                   INCOMING




                                CLIENT
DANGERS OF STORE & FORWARD                                                           13




• If target is offline for an extended period of timemessages can fill up the disk
  • Can cause a server to crash


• Especially problematic in B2B integration
  • 1 MB/message, 100 message/sec = 6GB/minute


• Solution – discard messages after a while
  •   Use [TimeToBeReceived("00:01:00")] on the message definition
REQUEST/RESPONSE                                                     14



                               SERVER




                               MSMQ
                    OUTGOING            INCOMING




Client can’t assume
when a response                                    Equivalent to 2
will arrive, if at all                             one-way messages




                               MSMQ
                    OUTGOING            INCOMING




                               CLIENT
REQUEST/RESPONSE                                                                  15




• Message is sent from the server to the client’s queue
  If the client is offline, message sits in the server machine’s outgoing queue

• Client is not blocked until response arrives

• If two requests were sent, responses may arrive out of order
WARNING! THIS IS NOT RPC                                                                16




• Do NOT try to implement regular request/response patterns on top of messaging
• The client should be designed so that it can continue operating if a response never
  comes


Differences from RPC
• RPC is easy to code
  • After invoking a web service
  • Next line of code assumes we’ve got a response


• RPC problems
  • Can’t reason about the time between one line of code and another


• Messaging makes this all explicit
MESSAGING AND NSERVICEBUS                  17



                       SERVER




                       MSMQ
            OUTGOING            INCOMING


   Transaction




                       MSMQ
            OUTGOING            INCOMING




                       CLIENT
DEFINE A MESSAGE                                                         18




• Preferably inherit from IEvent or ICommand


• Use IMessage when replying using Bus.Reply()


• Also possible to define your own convention
               •   Configure.DefiningMessagesAs(t=>MyOwnConvention(t))


• Add properties like a regular class/interface


• Keep contract definintions in their own assembly/project



public class MyEvent: IEvent              {}
INSTANTIATE A MESSAGE                                19




• var myMessage = new MyMessage();


• var myMessage = Bus.CreateInstance<MyMessage>();
SEND A MESSAGE                                               20




Bus.Send(messageObject);



Can instantiate and send together (useful for interfaces):

Bus.Send<IMessage>((message) =>
{
    message.Prop1 = value1;
    message.Prop2 = value2;
});
SPECIFY DESTINATION                                                    21




1. Bus.Send(destination, messages);
   Requires that application manages routing



2. Configure destination for message type.
   In <UnicastBusConfig>, under <MessageEndpointMappings> specify one of
   the following:
   - <add Messages="assembly" Endpoint="destination"/>
   - <add Messages="type" Endpoint="destination"/>


3. Specify destination using
   - QueueName@ServerName , or
   - Just QueueName for the same machine
HANDLE A MESSAGE                                                             22




Write a class that implements IHandleMessages<T> where T is a message type



public class MyHandler : IHandleMessages<MyMessage>
{
    public void Handle(MyMessage message)
    {


    }
}


Remember to specify in <UnicastBusConfig>, under
<MessageEndpointMappings> one of the following:
- <add Messages="assembly" Endpoint="source"/>
- <add Messages="type" Endpoint="source"/>
CONFIGURING AN ENDPOINT                                                                  23




When configuring an endpoint inherit from
1. Using AsA_Client will

   - use non-transactional MsmqTransport
   - purge its queue of messages on startup
   - processes messages using its own permissions, not those of the message sender


2. Using AsA_Server will

   - use transactional MsmqTransport
   - not purge its queue of messages on startup, hence fault-tolerant
   - processes messages using the permissions of the sender (impersonation)


3. Using AsA_Publisher will

   - extends AsA_Server
   - indicates to the infrastructure that a storage for subscription request is to be set up
EXERCISES                      24




ONE-WAY MESSAGING (CLIENT)


PROCESSING MESSAGES (SERVER)


EXCEPTIONS
UNIT TESTING MESSAGE HANDLERS                                                     25




Available from NuGet using


Install-Package NServiceBus.Testing


Provides the ability to set expectations around how message handlers handle messages
• Expect: Send, Reply, Publish, etc...


Test.Initialize();
Test.Handler<MyHandler>()
     .ExpectPublish<MyMessage>(message => message.Prop1 == value1)
     .OnMessage<SomeEvent>(someEvent =>
     {
        someEvent.Prop1 = inputValue1;
     });
EXERCISE       26




UNIT TESTING
SAGA                 27




• Definition
• Saga declaration
• Saga ending
• Saga testing
• Exercise
SAGA - DEFINITION                                                                28




A Saga:


•   Is a pattern for implementing long-lived transaction by using a series of
    shorter transactions


•   Holds relevant state to needed to process mulitple messages in a ”saga entity”

•   Are initiated by a message (event/command)
SAGA - DECLARATION                                                                29




public class MyPolicy : Saga<MyPolicyData>,
         IAmStartedByMessages<MyMessage1>,
         IHandleMessages<MyMessage2>
{
     public void Handle(MyMessage1 order)
     public void Handle(MyMessage2 order)
}



•   Methods are like regular message handling logic
•   Sagas can be started by multiple messages (IAmStartedByMessages<T>)
•   First messages should start saga, following messages should be processed by
    the same one
SAGA – DECLARATION CONT.                               30




public class MyPolicyData : ISagaEntity
{
       public Guid Id { get; set; }
       public string Originator { get; set; }
       public string OriginalMessageId { get; set; }
}
ENDING A SAGA                                                                        31




MarkAsComplete();


•   Can call this from any method
•   Causes the saga to be deleted
•   Any data that you want retained should be sent on (or published) via a message
UNIT TESTING A SAGA                                          32




Test.Saga<MyPolicy>()
    .ExpectPublish<Message1>(/* check values */)
    .ExpectSend<Message2>(/* check values */)
    .ExpectReplyToOriginator<Message3>(/* check values */)
    .When(saga => saga.Handle(myMessage));



/* check values */
message => return(message.Data == someValue);
EXERCISE - SAGAS ROCK   33
EXTRA EXERCISES               34




TIMEOUT
CUSTOM XML NAMESPACE
CONFIGURABLE ROUTING
DEPENDENCY INJECTION
WEB-APP HOSTING
FULL DUPLEX
DISTRIBUTION GROUP EXERCISE

More Related Content

PPTX
Message Oriented Architecture using NServiceBus
PPTX
NServiceBus
PPTX
NServiceBus - building a distributed system based on a messaging infrastructure
PPTX
NServiceBus introduction
PPTX
Aws Solution Architecture Associate - summary
PPTX
Content Delivery Network - CDN
PPTX
RabbitMQ interview Questions and Answers
PDF
Message Oriented Architecture using NServiceBus
NServiceBus
NServiceBus - building a distributed system based on a messaging infrastructure
NServiceBus introduction
Aws Solution Architecture Associate - summary
Content Delivery Network - CDN
RabbitMQ interview Questions and Answers

What's hot (20)

PPTX
Introduction to Microservices Patterns
PPTX
Azure Service Bus
ODP
Introduction To RabbitMQ
PPTX
Rabbit MQ introduction
PPTX
The RabbitMQ Message Broker
PDF
Rapport sécurité
PPTX
차세대 데이터센터 네트워크 전략
PDF
Alphorm.com Formation CCNP ENCOR 350-401 (5/8) : Architecture
PPTX
7 Steps to Threat Modeling
PPTX
20160517 eduroam for institution owned devices
PDF
Integration Patterns and Anti-Patterns for Microservices Architectures
PPTX
Aws security best practices
PDF
Platform as a Service (PaaS) - A cloud service for Developers
PPTX
Identity & access management
PPTX
Edgar f
PPTX
Intro to Azure Service Bus
PDF
Windows-Server-2022-Courseware.pdf......
PDF
Nagios, Getting Started.
PPTX
Upgrading to Exchange 2016
PPTX
06. security concept
Introduction to Microservices Patterns
Azure Service Bus
Introduction To RabbitMQ
Rabbit MQ introduction
The RabbitMQ Message Broker
Rapport sécurité
차세대 데이터센터 네트워크 전략
Alphorm.com Formation CCNP ENCOR 350-401 (5/8) : Architecture
7 Steps to Threat Modeling
20160517 eduroam for institution owned devices
Integration Patterns and Anti-Patterns for Microservices Architectures
Aws security best practices
Platform as a Service (PaaS) - A cloud service for Developers
Identity & access management
Edgar f
Intro to Azure Service Bus
Windows-Server-2022-Courseware.pdf......
Nagios, Getting Started.
Upgrading to Exchange 2016
06. security concept
Ad

Similar to Introduction to NServiceBus (20)

PPTX
NServiceBus workshop presentation
PDF
WSO2 Message Broker - Product Overview
PDF
IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...
PDF
WSO2 Product Release webinar - WSO2 Message Broker 2.2.0
PPTX
Making communication across boundaries simple with Azure Service Bus
PPTX
Docker Swarm secrets for creating great FIWARE platforms
PDF
Mini-Training: Message Brokers
PDF
FIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE Platforms
PDF
Rabbitmq an amqp message broker
PDF
IBM MQ: Managing Workloads, Scaling and Availability with MQ Clusters
PDF
Connecting IBM MessageSight to the Enterprise
PDF
Cloud Messaging Service: Technical Overview
PPTX
The Overview of Microservices Architecture
PPTX
zeromq
PDF
HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...
PDF
IBM MQ - better application performance
PPTX
Cloud computing Module 2 First Part
PDF
Enterprise Messaging With ActiveMQ and Spring JMS
PPTX
Azure Messaging Services #1
PDF
Connecting Applications Everywhere with ActiveMQ
NServiceBus workshop presentation
WSO2 Message Broker - Product Overview
IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...
WSO2 Product Release webinar - WSO2 Message Broker 2.2.0
Making communication across boundaries simple with Azure Service Bus
Docker Swarm secrets for creating great FIWARE platforms
Mini-Training: Message Brokers
FIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE Platforms
Rabbitmq an amqp message broker
IBM MQ: Managing Workloads, Scaling and Availability with MQ Clusters
Connecting IBM MessageSight to the Enterprise
Cloud Messaging Service: Technical Overview
The Overview of Microservices Architecture
zeromq
HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...
IBM MQ - better application performance
Cloud computing Module 2 First Part
Enterprise Messaging With ActiveMQ and Spring JMS
Azure Messaging Services #1
Connecting Applications Everywhere with ActiveMQ
Ad

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
Teaching material agriculture food technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
sap open course for s4hana steps from ECC to s4
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
“AI and Expert System Decision Support & Business Intelligence Systems”
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectral efficient network and resource selection model in 5G networks
Teaching material agriculture food technology
MIND Revenue Release Quarter 2 2025 Press Release
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Review of recent advances in non-invasive hemoglobin estimation

Introduction to NServiceBus

  • 1. NSERVICEBUS Inspired by: The course authored by Udi Dahan Oslo/Fagdag Espen Ekvang/Tomas Jansson 01/03/2013
  • 2. AGENDA 2 • Intro • Messaging and queues • Testing • SOA • Saga
  • 3. INTRO 3 • Fallacies of distributed computing • Why NServiceBus? • Bus vs. Broker • Service orientation • Excercises
  • 4. FALLACIES OF DISTRIBUTED COMPUTING 4 1. The network is reliable 2. Latency isn’t a problem 3. Bandwidth isn’t a problem 4. The network is secure 5. The topology won’t change 6. The administrator will know what to do 7. Transport cost isn’t a problem 8. The network is homogeneous Cant’ assume WHEN the message will arrive, IF AT ALL
  • 5. WHY NSERVICEBUS 5 1. The network is reliable 2. Latency isn’t a problem NServiceBus addresses the first five directly 3. Bandwidth isn’t a problem 4. The network is secure 5. The topology won’t change 6. The administrator will know what to do 7. Transport cost isn’t a problem 8. The network is homogeneous The most developer-friendly service bus for SOA on .NET
  • 6. BUS VS. BROKER 6 • Bus is not necessarily physically separate • Simpler; no routing or service fail over • No single point of failure App App Buss.dll Broker App App Buss.dll Buss.dll App App
  • 7. TENETS OF SERVICE ORIENTATION 7 • Services are autonomous • Share contract & schema, not class or type • Boundaries are explicit • Compatibitility is base on Policy
  • 8. LAYERS & COUPLING 8 Tight coupling Loose coupling Sales Shipping CRM UI BL DAL DB Referential Integrity Reintroduces coupling
  • 9. WHEN CAN I WRITE SOME CODE? 9 • Getting started • New class library • Install-Package NServiceBus.Host • Logging • NServiceBus uses log4net, you can configure logging in app.config • Default output is console
  • 10. EXERCISES 10 HELLO WORLD LOGGING
  • 11. MESSAGING AND QUEUES 11 • Store & forward • Dangers of store & forward • Request/Response • Messaging and NServiceBus • Exercises
  • 12. STORE & FORWARD 12 SERVER MSMQ OUTGOING INCOMING Store & Forward writes to disk Resilient in the face of failures MSMQ OUTGOING INCOMING CLIENT
  • 13. DANGERS OF STORE & FORWARD 13 • If target is offline for an extended period of timemessages can fill up the disk • Can cause a server to crash • Especially problematic in B2B integration • 1 MB/message, 100 message/sec = 6GB/minute • Solution – discard messages after a while • Use [TimeToBeReceived("00:01:00")] on the message definition
  • 14. REQUEST/RESPONSE 14 SERVER MSMQ OUTGOING INCOMING Client can’t assume when a response Equivalent to 2 will arrive, if at all one-way messages MSMQ OUTGOING INCOMING CLIENT
  • 15. REQUEST/RESPONSE 15 • Message is sent from the server to the client’s queue If the client is offline, message sits in the server machine’s outgoing queue • Client is not blocked until response arrives • If two requests were sent, responses may arrive out of order
  • 16. WARNING! THIS IS NOT RPC 16 • Do NOT try to implement regular request/response patterns on top of messaging • The client should be designed so that it can continue operating if a response never comes Differences from RPC • RPC is easy to code • After invoking a web service • Next line of code assumes we’ve got a response • RPC problems • Can’t reason about the time between one line of code and another • Messaging makes this all explicit
  • 17. MESSAGING AND NSERVICEBUS 17 SERVER MSMQ OUTGOING INCOMING Transaction MSMQ OUTGOING INCOMING CLIENT
  • 18. DEFINE A MESSAGE 18 • Preferably inherit from IEvent or ICommand • Use IMessage when replying using Bus.Reply() • Also possible to define your own convention • Configure.DefiningMessagesAs(t=>MyOwnConvention(t)) • Add properties like a regular class/interface • Keep contract definintions in their own assembly/project public class MyEvent: IEvent {}
  • 19. INSTANTIATE A MESSAGE 19 • var myMessage = new MyMessage(); • var myMessage = Bus.CreateInstance<MyMessage>();
  • 20. SEND A MESSAGE 20 Bus.Send(messageObject); Can instantiate and send together (useful for interfaces): Bus.Send<IMessage>((message) => { message.Prop1 = value1; message.Prop2 = value2; });
  • 21. SPECIFY DESTINATION 21 1. Bus.Send(destination, messages); Requires that application manages routing 2. Configure destination for message type. In <UnicastBusConfig>, under <MessageEndpointMappings> specify one of the following: - <add Messages="assembly" Endpoint="destination"/> - <add Messages="type" Endpoint="destination"/> 3. Specify destination using - QueueName@ServerName , or - Just QueueName for the same machine
  • 22. HANDLE A MESSAGE 22 Write a class that implements IHandleMessages<T> where T is a message type public class MyHandler : IHandleMessages<MyMessage> { public void Handle(MyMessage message) { } } Remember to specify in <UnicastBusConfig>, under <MessageEndpointMappings> one of the following: - <add Messages="assembly" Endpoint="source"/> - <add Messages="type" Endpoint="source"/>
  • 23. CONFIGURING AN ENDPOINT 23 When configuring an endpoint inherit from 1. Using AsA_Client will - use non-transactional MsmqTransport - purge its queue of messages on startup - processes messages using its own permissions, not those of the message sender 2. Using AsA_Server will - use transactional MsmqTransport - not purge its queue of messages on startup, hence fault-tolerant - processes messages using the permissions of the sender (impersonation) 3. Using AsA_Publisher will - extends AsA_Server - indicates to the infrastructure that a storage for subscription request is to be set up
  • 24. EXERCISES 24 ONE-WAY MESSAGING (CLIENT) PROCESSING MESSAGES (SERVER) EXCEPTIONS
  • 25. UNIT TESTING MESSAGE HANDLERS 25 Available from NuGet using Install-Package NServiceBus.Testing Provides the ability to set expectations around how message handlers handle messages • Expect: Send, Reply, Publish, etc... Test.Initialize(); Test.Handler<MyHandler>() .ExpectPublish<MyMessage>(message => message.Prop1 == value1) .OnMessage<SomeEvent>(someEvent => { someEvent.Prop1 = inputValue1; });
  • 26. EXERCISE 26 UNIT TESTING
  • 27. SAGA 27 • Definition • Saga declaration • Saga ending • Saga testing • Exercise
  • 28. SAGA - DEFINITION 28 A Saga: • Is a pattern for implementing long-lived transaction by using a series of shorter transactions • Holds relevant state to needed to process mulitple messages in a ”saga entity” • Are initiated by a message (event/command)
  • 29. SAGA - DECLARATION 29 public class MyPolicy : Saga<MyPolicyData>, IAmStartedByMessages<MyMessage1>, IHandleMessages<MyMessage2> { public void Handle(MyMessage1 order) public void Handle(MyMessage2 order) } • Methods are like regular message handling logic • Sagas can be started by multiple messages (IAmStartedByMessages<T>) • First messages should start saga, following messages should be processed by the same one
  • 30. SAGA – DECLARATION CONT. 30 public class MyPolicyData : ISagaEntity { public Guid Id { get; set; } public string Originator { get; set; } public string OriginalMessageId { get; set; } }
  • 31. ENDING A SAGA 31 MarkAsComplete(); • Can call this from any method • Causes the saga to be deleted • Any data that you want retained should be sent on (or published) via a message
  • 32. UNIT TESTING A SAGA 32 Test.Saga<MyPolicy>() .ExpectPublish<Message1>(/* check values */) .ExpectSend<Message2>(/* check values */) .ExpectReplyToOriginator<Message3>(/* check values */) .When(saga => saga.Handle(myMessage)); /* check values */ message => return(message.Data == someValue);
  • 33. EXERCISE - SAGAS ROCK 33
  • 34. EXTRA EXERCISES 34 TIMEOUT CUSTOM XML NAMESPACE CONFIGURABLE ROUTING DEPENDENCY INJECTION WEB-APP HOSTING FULL DUPLEX DISTRIBUTION GROUP EXERCISE