NServiceBus (NSB) is a popular framework for implementing service-oriented architectures (SOA) using C#. It can integrate with Windows Communication Foundation (WCF) web services to allow them to leverage NSB's capabilities like message encryption, retries, and sagas. NSB simplifies the coding of WCF contracts and hosting, and treats WCF endpoints like any other NSB endpoint. This allows management of WCF services through NSB tools and the use of NSB features in WCF workflows.
2. Why NServiceBus?
NServiceBus (NSB) is the most popular ESB for the
C# platform in existence.
It is decentralized and has many tools to assist in
deployment.
Many people may wonder the need to use an
ESB?
It is to manage services for any applications not
performing in the website.
3. Source code
My test source code can be found at
https://github.com/richhelton
Particular samples and souce code can be found at
https://github.com/Particular
4. For those that don't use
ESBs....
You may see many websites that say “Don't refresh this
page” for instance as they are website only centric that
may not consider an ESB.
You may also talk to some of their staff to know that they
do not acknowledge that server and network errors can
occur.
For those who want to monitor their services,
transactions, messages, and give your website the ability
to refresh a page after you enter a credit card, we have
NSB.
5. What is a Service-Oriented
Architecture
SOA is a combination of design patterns, usually
implemented through frameworks.
SOA combines distinct services that are
communicated through messages to cooperate as
a single end-to-end business system. A service
could be considered a Windows service, or a
distinct unit of work, such as a mainframe job.
7. More SOA Advantages
By using services, we may start/stop services
based on performance.
We may get running totals on the number of
successful and error messages.
8. What is a Enterprise Service
Bus (ESB)?
ESB is a software architecture design model in
which a common bus is shared across a framework
to allow common agreed upon communication
between different endpoints or services.
10. Software Quality
Software Quality are cross-cutting features of the
software, that normally defines the technical
requirements of the software itself.
These qualities are usually security, re-use,
maintainability, reliability, efficiency, and other
characteristics that make one want to use the
framework.
Notice that NSB match these qualities one-to-one.
11. SOA and ESBs to the rescue
In the Microsoft world, when discussing a
framework that implements Service Oriented
Architecture (SOA), one may think of the Windows
Communication Foundation (WCF) web services.
12. SOA and ESBs to the rescue
In the Microsoft world, when discussing a
framework that implements Service Oriented
Architecture (SOA), one may think of the Windows
Communication Foundation (WCF) web services.
13. Benefits for NSB
Separate business logic between services.
High availability of services.
Highly configurable.
Message durability in guaranteed delivery.
Viewing and monitoring messages and services give an
end-to-end viewpoint.
Retries for sending of messages.
Message workflows in the form of Sagas.
Heartbeats to give real-time status.
Easy encryption of messages and data.
NSB Hosting and installation tools.
15. The Message
NSB is built to send messages on a queue using a variety
message queues and techniques, along to various
configurations for almost any type of Service. First we start
with messages.
IMessage – is a basic message interface.
IEvent – used mostly for publish-subscribe, and is a type
of IMessage.
ICommand – used mostly for request-response, and is a
type of IMessage.
17. The Bus
NSB is built on configuring its ESB bus called IBus, and I can
be configured for multiple queue and persistence,
endpoint, services and message scenarios. A very basic
configure.
18. The Message handler
NSB sends messages through the Send() or Publish()
methods, but it needs a message handler to be listening
on on the message queue to process the message. A
message handler will listen on an endpoint looking for a
particular message.
The endpoints and message that it is listening for is
defined in the App.config, and in the message handler
function.
21. Additional Tools for NSB
Service Matrix is a graphical interface extension in Visual
Studio for rapid development.
Service Pulse is a production monitoring tool for
heartbeats and messages.
Service Insight provides deep insight into endpoints,
messages and services.
22. ServiceMatrix
Service Matrix is a graphical interface extension in Visual
Studio for rapid development. It will generate code from
models.
24. ServicePulse
Getting heartbeats and custom messages for Production.
It is installed as a service that is connected through a URL
http://localhost:9090/#/dashboard
28. Message Features
NSB supports many message models such as request-response
and publish-subscribe.
Messages do not have to be only messages for the NSB
Bus, but NSB integrates into databases and web services
as well for third party integration.
NSB can also be used to integrate into File I/O , as well as
the Secure File tranfer Protocol.
29. Publish-subscribe
NSB supports publish-subscribe, where a publisher puts
the message on a queue where multiple subscribers may
read it off the queue.
30. Request-response
NSB supports request-response, where a requester sends
a message that the replier receives and responds with a
different message.
31. Saga services
NSB supports sagas, the ability to save message state and
respond back to the originator with changes. This is saving
message state.
32. Timeout Message
NSB supports timeout messages, the ability to set a timer
in seconds, minutes, etc., or time of day to send a special
message that a timeout has occurred, and to process the
action, such as delete a message, start a job, and more.
Here, we schedule a task every minute.
public void Handle(ScheduleATask message)
{
Console.WriteLine("Scheduling a task to be executed every
1 minute");
Schedule.Every(TimeSpan.FromMinutes(1)).Action(() => bus.
SendLocal(new ScheduledTaskExecuted()));
}
33. Message Mutators
NSB supports message mutators, the ability to mutate, or
change, messages during runtime as the message is
transmitted through different endpoints. This is valuable
tool for adding information, such as debugging, or
informational, information as it is running.
34. Message Encryption
NSB supports message encryption, the ability to encrypt
pieces or the whole of messages and data. The default
encryption used by NSB is AES.
35. Scaleout
NSB supports scaleout messaging for clustering, this is the
ability to have multiple worker services, that are clones of
the same codebase, with a single sender, to offload the
performance to multiple machines.
36. Performance Monitoring
NSB supports performance counters integration into the
Windows Performance Monitor to get statistics on
endpoints, messages and services.
37. Gateways
NSB supports gateways, which is the ability to send
messages through an HTTP/HTTPS tunneling, in scenarios
where HTTPS is needed for security and messages need to
pass through these protocols to meet firewall
requirements.
38. Databus
NSB supports databus, which is the ability to attach a file
to a link, or attachment, with the message, because the
file is too large to be enclosed in the message itself.
39. Sagas, What are they
good for?
October 10, 2014
By Rich Helton
40. Saga Features
Sagas can save state of messages into a data store.
Sagas can route messages based on message state.
Sagas are event driven to be started by messages.
Sagas contain timers to execute events on messages, the
timer can be a periodic time such as every hour, or to
execute at a certain date and time.
41. Sagas intercept and send
messages
Sagas are started by messages, they save data, and send
messages. As they intercept messages, they can read and
save state based on the message ID to add changes to the
message, creating workflow.
43. Saga saves data
Sagas will save data from a message in the form of the
IContainSaga interface.
44. Saga maps data
Sagas will map the data, depending on the IBus
configuration, from/to the message to/from the Saga Data
object.
45. Saga handle messages
Sagas are started by at least one message and handler
messages.
46. Saga data from messages
Sagas can save data, the Data object, associated with a
message key to get details on the end-to-end workflow of
the message
47. Saga data from messages
Sagas can retrieve data associated with a message key to
get details on the end-to-end workflow of the message
48. ConfigureHowToFindSags()
Sagas need to know how to lookup messages, the key
association, usually from an ID in the message, the
ConfigureHowToFindSaga() performs this function.
52. Persistence Features
NSB supports many methods to queue and persists
messages.
NSB can save subscription information for publish-subscribe
in various models.
NSB can save Saga data information into various models.
NSB takes care of the mapping of the data objects.
54. Persistence interfaces
In-Memory – items that are persisted in the the memory
of the service itself.
RavenDB – the default for many items like subscriptions,
items are persisted in a local Raven database.
MSMQ – the default for messages, items are persisted in
the Microsoft Message Queuing.
NHibernate – a .NET Hibernate framework to map
objects to relational databases, to include SQL Server,
MySQL, and many others.
55. What is persisted?
Timeouts – items that keep track of timeouts. Default is
RavenDB.
Subscriptions – the information to keep track of the
subscriber information in publish-subscribe. Default is
RavenDB.
Sagas – the information stored for Saga data from
messages. Default is RavenDB.
Gateway – the information to keep track of gateway
messaging. Default is RavenDB.
Distributor – the messages that is sent to different
workers. Default is MSMQ.
56. Messages
Messages can also be persisted in different types of
queues.
By default, NSB sends messages in MSMQ.
However, by using NHibernate, SQL queuing could just as
easily be used to send messages.
59. Feature of WCF
The Windows Communication Foundation (WCF) is a
Microsoft framework for establishing communications
between endpoints, usually between web services.
Communication can happen over HTTP, TCP, IPC,
or even MSMQ.
60. The ABCs of WCF
WCF needs specific information to establish a connection
to exchange data:
Address – the URL address to establish a connection.
Binding – which defines the types of services that provide
connections, include security and encoding settings.
Contracts – defines the operational, the API functions, and
data that will be available from the server to the clients.
61. Contracts
There are three main types of contracts:
Service – defines the overall web service starting point.
Operational – defines the available methods from the
service.
Data – defines the available data, through serialization,
that is available from the endpoints, usually in the method
types.
63. Data Contracts
The [DataContract] defines the available data from the
service.
64. The address
The address, or available URL, is defined for the server in
its App.config or Web.config.
65. The binding type
The binding type is defined for the server in its App.config
or Web.config.
We are using the basic http binding and mex http binding
as well to expose the endpoint to the client.
66. Many binding types
There many binding types in WCF as there are many
different transport and message types.
A list can be found at
http://msdn.microsoft.com/en-us/library/ms730879(v=vs.110).
67. SVC Config Editor
We can graphically configure the App.config, or
Web.config if IIS app, using the service configuration
editor.
Opening it in Visual Studio,
69. Seeing the contract
The client will need import the contracts in the
form of a proxy to communicate to the server.
One of the forms of importing web service
interfaces is the Web Service Definition Language
(WSDL).
70. The WSDL is online
In most cases the WSDL is online, as it defines the
interfaces needed for the clients.
71. Viewing the WSDL
We can see that the WSDL has many of the ABC web
service components to connect to the web service server.
72. Adding the Service Reference
We can add the Service Reference to the client to use the
interfaces to communicate to the server.
75. Adding Tracing
The WCF Services, be it client or server, can have trace
listeners to be added to the application as it runs.
Microsoft provides a Service Trace Viewer to view
these messages, as the interaction can contain
WCF specific pieces.
See http://msdn.microsoft.com/en-us/
library/ms732023(v=vs.110).aspx
76. Configure Tracing
We can configure the tracing through the service
configuration editor, to store in the App.config.
77. Viewing Tracing
This will allow us to get information on endpoints,
such as the messages.
78. Hosting WCF
The WCF Server listens for incoming requests from the
WCF client. Therefore, it should be available when a client
needs to connect.
The WCF Service can be running as a Windows Service.
In an IIS application.
Self-Hosted as a .NET application, usually with a
command window.
Or as a Windows Process Activation Service
(WAS).
80. NSB support of WCF
NSB supports WCF integration.
The benefits of using NSB with WCF are:
NSB simplifies the coding of contracts of WCF, as
well as hosting.
This saves on development time.
A web service endpoint can be handled as any
other NSB endpoint.
This allows all the other benefits of NSB in WCF,
such as message encryption and retries.
81. NSB-WCF messages
Messages in NSB are defined with the IMessage
interface. This can be done instead of a data
contract in WCF.
public class PaymentMessage : IMessage
{
public Guid EventId { get; set; }
public PaymentReq paymentReq { get; set; }
}
82. NSB-WCF operations
Instead of using operations in WCF, NSB uses
message handlers to receive a defined message
and process it, and in many cases send back a
message response.
83. NSB-WCF-Saga
Becuase NSB supports Saga workflows, retries,
timeouts, tools for production, rapid development,
and managing services, these tools can be applied
to WCF web services, and we can use NSB to retry
a web service, error report on it, manage the
service, and use Saga workflow to manage the
business logic in the WCF web service.
85. NSB hosting features
NServiceBus.Host.exe is an executable to install, and
uninstall, DLL's using NSB as Windows services.
This includes features to add additional configurations
during runtime, to include endpoints.
The NServiceBus.Host framework and executable will
streamline the installation process by creating endpoints.
It will handle the Windows service installation and provide
NSB management features as well.
86. Host and Visual Studio
NServiceBus.Host.exe is an executable that can be used as
a reference in Visual Studio.
This is so the service can be DLL, it can generate endpoint
template code, and run the DLL in Visual Studio.
87. Debug
In the Debug properties of the project, we can see
NServiceBus.Host.exe running the DLL that is created.
89. The Host has several
deployments
NServiceBus.Host.exe has several configurations to run
worker services, installations, creating endpoints, and
much more.