Overview
Features
NServiceBus
http://www.nservicebus.com/GettingStarted.aspx — is simple framework that provides you simple API for queues and implements the ESB
http://en.wikipedia.org/wiki/Enterprise_service_bus model. With NServiceBus you could construct your application in SOA style
http://en.wikipedia.org/wiki/Service-oriented_architecture.
NServiceBus supports SAGAS — The Saga is a pattern that addresses these challenges uncovered by the relational database community years ago, packaged in NServiceBus for ease of use by developers.
http://www.nservicebus.com/Sagas.aspx
NServiceBus by default use for transport MSMQ
http://en.wikipedia.org/wiki/Microsoft_Message_Queuing so it supports transactions, durability, distribute transactions, retry logic and encryption from the box.
From the box NServiceBus supports work on HTTP for communicate over internet
You can find good video guide about NServiceBus
http://www.techdays.ru/videos/2295.html
Performance
It bases on MSMQ. So it performance wholly depends on MSMQ performance. On same envoriments the NServiceBus performance could be compared with WCF, BizTalk and Neuron ESB in some cases NServiceBus can be faster, for example:
http://www.udidahan.com/2008/05/21/nservicebus-performance/
Monitoring
As a part of the NServiceBus installation, a new performance counter called "Critical Time" is added underneath the new "NServiceBus" category. All processes running with the NServiceBus Host collect this information but if you want them to expose it you need to include "NServiceBus.PerformanceCounters" as one of the arguments to the host process. You can find more information on how to do this here.
In additional you could use embedded in MSMQ counters.
Additional information you could find here
http://www.nservicebus.com/Profiles.aspx
Install
- Download zip with code and examples from http://www.nservicebus.com/Downloads.aspx
- If you use Windows 7, Windows 7 users, make sure to unblock the zip file by right-clicking the downloaded zip, selecting properties, and pressing "Unblock".
- Open example project from folder zip folder\your net version\examples\PubSub. After open please run (press F5 in Visual Studio) if you see 3 console windows NServiceBus works correctly.
- This batch file that ensures that MSMQ is installed on your machine and is configured correctly. It also configures the DTC service and installs some performance counters.
How use
Define your message
Any messages in NServiceBus are POCO objects but they should support markered interface IMessage:
[Serializable]
public class EventMessage : IMessage {
public Guid EventId { get; set; }
public DateTime? Time { get; set; }
public TimeSpan Duration { get; set; }
}
Configure publisher
<MsmqTransportConfig
InputQueue="MyPublisherInputQueue"
ErrorQueue="error"
NumberOfWorkerThreads="1"
MaxRetries="5"
/>
<UnicastBusConfig
DistributorControlAddress=""
DistributorDataAddress=""
ForwardReceivedMessagesTo="">
<MessageEndpointMappings>
</MessageEndpointMappings>
</UnicastBusConfig>
Define publisher
public class ServerEndpoint : IWantToRunAtStartup {
public IBus Bus { get; set; }
public void Run() {
while (Console.ReadLine() != null) {
var eventMessage = publishIEvent ? Bus.CreateInstance() : new EventMessage();
eventMessage.EventId = Guid.NewGuid();
Bus.Publish(eventMessage);
}
}
public void Stop() {
}
}
Subscriber
Configuration
You could use two ways for configure your application: over xml and over code. Let's use second way:
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization {
public void Init() {
NServiceBus.Configure.With()
.CastleWindsorBuilder() // just to show we can mix and match containers
.XmlSerializer()
.UnicastBus()
.DoNotAutoSubscribe(); //managed by the class Subscriber2Endpoint
}
}
Message handler
public class EventMessageHandler : IHandleMessages {
public void Handle(IEvent message) {
Logger.Info(string.Format("Subscriber 2 received IEvent with Id {0}.", message.EventId));
}
private static readonly ILog Logger = LogManager.GetLogger(typeof (EventMessageHandler));
}
Saga
State
Using NServiceBus, you can explicitly define the data used for this state by implementing the interface IContainSagaData - all public get/set properties will be persisted by default:
public class MySagaData : IContainSagaData {
// the following properties are mandatory
public virtual Guid Id { get; set; }
public virtual string Originator { get; set; }
public virtual string OriginalMessageId { get; set; }
// all other properties you want persisted - remember to make them virtual
}
NServiceBus uses NHibernate to transparently store your saga data in a database. It can also automatically generate the database schema for storing these classes (through the use of Fluent NHibernate). You can, as always, swap out these technologies - just implement the IPersistSagas interface.
Adding behaviour
public class MySaga : Saga, IHandleMessages, IHandleMessages {
public void Handle(Message1 message) {
// code to handle Message1
}
public void Handle(Message2 message) {
// code to handle Message2
}
}