Contents
|
Hosting NServiceBus in your own Process
Lighter-weight than BizTalk & more powerful than WCF,
Requiring as little as 3 assemblies to be referenced Assembly references
The 3 assemblies you need to reference in order to host NServiceBus in your own process are shown to the left. Log4Net is the industry-standard logging library used by NServiceBus. NServiceBus.dll contains the main interfaces developers should be programming against, and NServiceBus.Core.dll contains all the runtime elements needed for execution. The AsyncPages sample demonstrates this configuration. NServiceBus InitializationIn the Application_Start method of your Global.asax file in a web application, or in the Main method of your Program file for console or Windows Forms applications, include the following initialization code:
NServiceBus.Configure.With() //for web apps this should be WithWeb()
.Log4Net()
.SpringBuilder()
.XmlSerializer()
.MsmqTransport()
.UnicastBus()
.LoadMessageHandlers()
.CreateBus()
.Start();
This is the minimum initialization code that is required by NServiceBus. Most of the methods you see here are extension methods on the NServiceBus.Configure class provided by the specific components that are packaged in the NServiceBus.Core assembly. This makes it possible for you to similarly configure your own components in the same style by writing your own extension methods.
In addition to the above initialization code, NServiceBus requires certain configuration data to be available. By default, it retrieves this information from the application config file, though this can be changed by using the CustomConfigurationSource() method. ConfigurationWhen using the initialization code above, you will need to provide configuration for the MsmqTransport - specifically the local queue from which it will receive messages, the number of threads it runs, and where it sends messages that couldn't be processed. You'll need to include this configuration section: <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core"/> And specify the configuration data as follows: <MsmqTransportConfig InputQueue="MyWebClient" NumberOfWorkerThreads="1" MaxRetries="5" ErrorQueue="error" /> In the case where an exception was thrown during the processing of a message, NServiceBus automatically retries the message (as it could be that if failed due to something transient like a database deadlock). MaxRetries specifies the maximum number of times this will be done before the message is moved to the ErrorQueue. Routing ConfigurationWhile you can tell NServiceBus to which address to send a message using the API: Bus.Send(toDestination, message); NServiceBus enables you to keep your code decoupled from where endpoints are deployed on the network through the use of routing configuration. In order to make use of it, include this configuration section: <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core"/> And then specify the configuration data like this: <UnicastBusConfig> <MessageEndpointMappings> <add Messages="MessageDLL" Endpoint="DestinationQueue@TargetMachine"/> </MessageEndpointMappings> </UnicastBusConfig> This tells NServiceBus that all messages in the MessageDLL assembly should be routed to the queue called DestinationQueue on the machine TargetMachine. This allows you to send messages from that assembly like this: Bus.Send(messageFromMessageDLL); Next StepsWith this basic setup done, you can now go and do publish/subscribe messaging. |