Top Banner
NServicebus Rich Helton June 14, 2012 Buy “Mastering NServiceBus with Persitence”
67
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: NServiceBus

NServicebus

Rich HeltonJune 14, 2012

Buy “Mastering NServiceBus with Persitence”

Page 2: NServiceBus

Introduction

• By using the .NET 4 Framework, with the combination of the Windows Communication Framework (WCF), Workflow (WF), Model-View-Controller (MVC3), Entity Frameworks (EF4), Azure, AppFabric, and Message Queuing, I believe that Microsoft has brought serious competition to compete against the entire spectrum of all other Enterprise Development Systems.

Page 3: NServiceBus

Introduction 2•Now Microsoft systems can be virtualized to

be decoupled form the hardware, be stripped down to run minimum services for the applications, and have many diagnostic and management tools. • This turns the Operating System into an

Application Server to be reckoned with from the Application Server vendors.• This could also creates software quality that

was never as common as it is now in the Industry, see http://en.wikipedia.org/wiki/Software_quality

Page 4: NServiceBus

The need for ESB

• An ESB, is a software architecture model used for designating and implementing the interaction and communication between mutually interacting software applications in Service Oriented Architecture, http://en.wikipedia.org/wiki/Enterprise_service_bus

Page 5: NServiceBus

The need for ESB

• The Bus, is the combination of software that queues messages, routes messages and processes messages to the correct endpoints. • In order to do this ESB software will usually

use other frameworks that handle message queuing and standard message queuing mechanisms. • For instance, NServiceBus can use RabbitMQ

or MSMQ.

Page 6: NServiceBus

Some benefits of ESB

• The message bus provides both standardization and integration of messages across an enterprise.• A message could be sent from any endpoint,

be it a database or service, and the messages are routed and interpreted to other services through the bus.• The services could be redundant as they could

come from any endpoint. • Even though the messages could be complex

and coming from endpoints that are different, such as a mainframe to SQL database, the bus is meant to translate their usage.

Page 7: NServiceBus

NServiceBus

•NServiceBus is an Open Source Enterprise Service Bus for .Net, http://nservicebus.com/ • Commercial licenses are required based on

messages per second, http://nservicebus.com/License.aspx • For the basic code, it can be pulled from

https://github.com/NServiceBus/NServiceBus

Page 8: NServiceBus

Other Choices• There are many ESB choices, but price and

documentation, including blogs and samples usually drive my choice. • For .NET, there is additionally BizTalk,

MassTransit, KeystrokeESBNet and SimpleServiceBus. • There are many sites like

http://en.wikipedia.org/wiki/Comparison_of_business_integration_software

Page 9: NServiceBus

AppFabric• Adding a caching provider and a monitoring

service to WCF and WF creates the Microsoft AppFabric Framework and Architecture.• There is a special version that supports Azure.• The Micrsoft.ServiceBus.dll and namespace

will be used to extend Service Bus helper extensions to WCF and WF.

Page 10: NServiceBus

Biztalk• Biztalk uses a Micrsoft ESB Toolkit to build

ESB. This Toolkit intergrates into Visual Studio 2010. http://technet.microsoft.com/en-us/library/ff699598.aspx •NServiceBus also has modeling and Code

Snippets that integrate into Visual Studio 2010 as well, more work has been done into Visual Studio 11.

Page 11: NServiceBus

Other Choices• Even other choices include not to utilize ESBs

at all, but use other technology like Windows Communication Foundation Workflow Services for SOA Binding Services and MSMQ as a message queue system to build a message system. • But standard Message Queue and ESB

frameworks provide the pieces that are already built and tested. • The downside is that you may be locked into

the technology and the roadmap that the framework uses.

Page 12: NServiceBus

NServiceBus

•NServiceBus is an Open Source Enterprise Service Bus for .Net, http://nservicebus.com/ • Commercial licenses are required based on

messages per second, http://nservicebus.com/License.aspx • For the basic code, it can be pulled from

https://github.com/NServiceBus/NServiceBus

Page 13: NServiceBus

NServiceBus-Intro

Page 14: NServiceBus

Getting Started

• The benefit of using a ESB package is that it will normally handle the messaging packages for you, NServiceBus handles a lot of the MSMQ and DTC complexities. • See

http://nservicebus.com/GettingStarted.aspx ,http://nservicebus.com/GettingStarted2.aspxhttp://nservicebus.com/GettingStarted3.aspx

Page 15: NServiceBus

Getting Started

• The installation is usually just running the RunMeFirst.bat file with the correct Administrator permissions.

Page 16: NServiceBus

Getting Started• You will be prompted to install NServiceBus

Studio for building NServieBus projects in Visual Studio, this plugin is found under /tools:

Page 17: NServiceBus

NServiceBusStudio• See

http://www.i-m-code.com/blog/blog/2012/03/15/nservicebus-vs-modeling-tools/• Also an example on

http://nservicebus.com/GettingStarted.aspx

Page 18: NServiceBus

Core Files

• There are many libraries included with NServiceBus, some for custom configs, and many samples to include from GitHub, but basic 4 libraries included for almost all apps are; log4net.dll, NServiceBus.Core.dll, NSeviceBus.dll, and NServiceBus.Host.exe. •NServiceBus is heavily reliant on Apache’s

log4net for default logging and the correct version must be installed.

Page 19: NServiceBus

Endpoint

•Normally every NServiceBus has one Endpoint, which is the Queue name to read or write messages. • If not defined, by default, the current

namespace of the project will be used. This method is sometimes used on Servers reading queues. • The name will be followed by an “@” with a

servername to be server specific.

Page 20: NServiceBus

Endpoint

• There are different conventions and ways to define an Endpoint, I found that this will also depend on the version of NServiceBus being used, http://andreasohlund.net/2012/01/27/convention-over-configuration-in-nservicebus-3-0/

Page 21: NServiceBus

Endpoint – FullDuplex Example

• App.config will normally define the endpoint in the UnicastBusConfig, here we define the Message as well in the Client:

Page 22: NServiceBus

Endpoint – FullDuplex Example

• Instead of using App.config, the configuration can be programmatic by building a class using IProviderConfiguration:

Page 23: NServiceBus

Endpoint from different places

• So, we could put a UnicastConfigBusConfig, for messages and an endpoint in an App.config, and a MessageForwardingInCaseOfConfig like so:

Page 24: NServiceBus

How do the different configs come together• The purpose of all the configs is simply to

build an IBus instance to send or receive the messages.• The Bus is normally created by a Handler or

building the bus directly with an arrangement of Builder calls.

Page 25: NServiceBus

A sample Builder for SendOnly

•Note : The order of which calls happen first can change the functionality. • This allows tighter control of the Bus.

Page 26: NServiceBus

A sample Handler for a Reply

Page 27: NServiceBus

Send versus Publish

• After the Bus is built with the configs and handlers, the clients and server need to Send, Publish or Reply to messages. • See http://mookid.dk/oncode/archives/807 ,

NServiceBus for Dummies who want to be Smarties for some of the differences.

Page 28: NServiceBus

Send with a Destination Server as it sends•Notice the “someserver”:

Page 29: NServiceBus

Building the IBus

Page 30: NServiceBus

Start with the Builders

•We will start with Configure.With() from and a builder type:

• The builders can be Defaultbuilder(), AutoFacBuilder(), CastleWindsorBuilder(), NinjectBuilder(), SpringBuilder(), StructureMapBuilder(), and UnityBuilder().

Page 31: NServiceBus

Information on Builders

• These Builder Objects are the containers in which to build the bus and configurations. http://nservicebus.com/Containers.aspx • First the configurations will be declared for

endpoints, transactional, type of container and then the CreateBus() and Start() will initiate the bus.

Page 32: NServiceBus

Types of Builders/Containers

• AutofacBuilder() – is the default container, same as DefaultBuilder() , that will use internal extensions in NServiceBus.• CastleWindsorBuilder() – Castle Windsor is an

Inversion of Control container. http://en.wikipedia.org/wiki/Castle_Project •NinjectBuilder() – A Inversion of Control

container, sometimes called the ninja of dependency injectors, http://ninject.codeplex.com/wikipage?title=What%20Is%20Ninject?

Page 33: NServiceBus

Types of Builders/Containers

• SpringBuilder() – A Inversion of Control container for Spring.net. http://www.springframework.net/ • StructureMapBuilder () – A Inversion of

Control container for StructureMap, http://docs.structuremap.net/. •UnityBuilder() – A Inversion of Control

container, Dependency Injection container from Microsoft. http://msdn.microsoft.com/en-us/library/dd203101.aspx

Page 34: NServiceBus

Next is normally the Serializers

•Next is normally the Serializers, this will transfer the message as binary or xml data.

• XmlSerializer() – Serializes the objects into an XML format, but there are limitations with the XML serialization in NServiceBus with object lists, Derived Objects, and arrays of objects, http://stackoverflow.com/questions/2816895/nservicebus-serization-issue-of-derived-types , the work around is to use the Binary serialization or to change the objects to account for these issues.

Page 35: NServiceBus

Next is normally the Serializers

• BinarySerializer() – Serializes the objects into an binary format, and is done with the standard .net binary serializer, http://nservicebus.com/Performance.aspx • JSonSerializer() – Javascript Object Notation

(JSON) format serialization, uses the Newtonsoft.json interface, http://json.codeplex.com/ .

Page 36: NServiceBus

Next is normally the Transport

•Next is normally the Transport, this is the queuing mechanism, and in many cases will be MSMQ.

• Transports can be created using ISendMessages and IReceiveMessages. A Service Broker example can be seen in NserviceBus-Contrib that includes a Sample https://github.com/NServiceBus/NServiceBus-Contrib

Page 37: NServiceBus

Transport Types•MsmqTransport() – This is the MSMQ

Transport mechanism commonly used, there are many samples, see FullDuplex example.• FtpTransport() – This is a Transport to send

messages across FTP, which has to have a directory, username and password included for FTP setup, see the FtpSample. • AzureQueueMessage() – Using Storage

Queues for Azure, see the Azure examples.

Page 38: NServiceBus

UnicastBusConfig•Now UnicastBus() must be defined:

•UnicastBus() tells NServiceBus to use unicast messaging. Used out of the box, see http://nservicebus.com/SelfHosting.aspx•Here we have a UnicastBusConfig defining the

message and endpoint in App.config client:

Page 39: NServiceBus

Now for the Bus• So far these have only been configurations

and the bus still hasn't been created, to create the bus, CreateBus() must be used, and to start the bus, then Start() must be used,

Page 40: NServiceBus

For SendOnly• For SendOnly(), createbus and start are not

needed because not all the components are needed from the bus.

Page 41: NServiceBus

Publish with Installers

Page 42: NServiceBus

Why I looked at SendOnly• I started looking at SendOnly because Publish

was not recommended for the Web interfaces. http://www.make-awesome.com/2010/10/why-not-publish-nservicebus-messages-from-a-web-application/ • Publishing requires a Subscription storage and

the messages are normally handled as transactional events, http://nservicebus.com/PubSubApiAndConfiguration.aspx

Page 43: NServiceBus

Subscription Storage• If multiple machines share the same

subscription storage, then Database Storage needs to be used, .DBSubscriptionStorage().• The Nhibernate connection strings will need

to be defined in the App.config.

Page 44: NServiceBus

Subscription Storage• If multiple machines share the same

subscription storage, then Database Storage needs to be used, .DBSubscriptionStorage().• The Nhibernate connection strings will need

to be defined in the App.config.

Page 45: NServiceBus

Subscription Storage• If multiple machines share the same

subscription storage, then Database Storage needs to be used, .DBSubscriptionStorage().• The Nhibernate connection strings will need

to be defined in the App.config.

Page 46: NServiceBus

Subscription Storage• The Manufacturing Example shows an MSMQ

Storage example and in memory:

Page 47: NServiceBus

Installers• In the Manufacturing and AsyncPages, there

is the Install() for the installation of endpoints and infrastructure, such as MSMQ and RavenDB.

• See http://andreasohlund.net/2012/01/26/installers-in-nservicebus-3-0/

Page 48: NServiceBus

MSMQ

Page 49: NServiceBus

NServiceBus supports many types of messaging Schemes•NServiceBus supports messaging through

databases, like its native RavenDB, or SQL Server. • It also supports Message Queuing

mechanisms like Microsoft Message Queue and Apache’s RabbitMQ.

Page 50: NServiceBus

MSMQ

•MSMQ is the Message Queuing System built into the Microsoft Operating System since 1997.• The Server Operating Systems, like Windows

Server 2008, offers additional features. • See

http://en.wikipedia.org/wiki/Microsoft_Message_Queuing

Page 51: NServiceBus

Viewing MSMQ• It is important to administer the MSMQ

Queues when building MSMQ applications. If the Queue is not built, the application could error. • The Queue will normally store its data in XML

or Binary form. XML makes the data readable for troubleshooting.

Page 52: NServiceBus

Viewing MSMQ•While there is the Visual Studio View Servers,

he MSMQ MMC, and even the Computer Management way to view Queues.• I still like to use IMQT to view the XML, a

commercial tool is Queue Explorer. IMQT is http://utvecklargodis.blogspot.com/2007/03/msmq-management-tool.html

Page 53: NServiceBus

Viewing MSMQ in Visual Studio

Page 54: NServiceBus

Viewing MSMQ in IMQT

Page 55: NServiceBus

Programming MSMQ

•Windows will use the System.Message namespace to program the MessageQueue class.•NServiceBus offers extensions of this class as

MSMQUtilities for managing Queues and MSMQInstallation for installing MSMQ. • These are in the NServiceBus.Utils

namespace, found in the NServiceBus.Utils.dll. • Code for MSMQUtilities can be found at

https://github.com/NServiceBus/NServiceBus/blob/master/src/utils/MsmqUtilities.cs

Page 56: NServiceBus

Programming MSMQ•Here is a small example to check if a Queue is

built, and if not build it with the Administrator username using both System and NServiceBus Utils for the client, the server should create its own queue by default:

Page 57: NServiceBus

Unit Tests

Page 58: NServiceBus

Unit Testing

•NServiceBus normally uses NUnit to test with handlers that it already has built in for Sends and Publish. Nunit can be found at http://www.nunit.org/ • The code for the Handlers can be found at

https://github.com/NServiceBus/NServiceBus/blob/master/src/testing/Handler.cs

Page 59: NServiceBus

Unit Testing• To test Nunit in Visual Studio, you will likley

need to install a plugin like TestDriven.Net, Resharper, or Ncoverage., and the nunit framework will have to be installed.

Page 60: NServiceBus

Handlers•Here’s what a typical Test Handler looks like to

test if the message and header is validated:

Page 61: NServiceBus

MOQ

• Another choice besides using the NServiceBus custom handlers is to use MOQ. •MOQ is used to mockup a bus, MOQ came

from http://code.google.com/p/moq/ • There are many links for doing this, one with

some sample test code can be found at http://pastebin.com/CSrN6fR1

Page 62: NServiceBus

Deploying

Page 63: NServiceBus

Installing NServiceBus as Service

• It was mentioned earlier that NServiceBus.Host.Exe is one of the 4 files to always be included with NServiceBus.• This is the file used for running NServiceBus

applications that are non-Console based. • This file is used for installing, running and

debugging NServiceBus applications. • See http://nservicebus.com/GenericHost.aspx

Page 64: NServiceBus

NServiceBus.Host.exe debug

•NServiceBus.Host.Exe is needed to debug the applications:

Page 65: NServiceBus

NServiceBus.Host.exe commands

• A service can be created at the command line by using NServiceBus.Host.exe /install /serviceName:MyServer.dll /displayName:"My Super Duper service" /description:"My server installed by NService Magic”

• See http://prashantbrall.wordpress.com/tag/nservicebus/ for further details on doing this.

Page 66: NServiceBus

Questions?

Page 67: NServiceBus

Appendix