-
Notifications
You must be signed in to change notification settings - Fork 0
Home
We aim to provide you with sample code and explanations for some the scenarios we are about to go through in the workshop. We will give a short presentation on stage, presenting the various benefits of Microsoft Message Queuing , and its use in NServiceBus. More detailed information, and example code can be found here on this wiki, or in the repository itself. We expect people to run into various technical difficulties, and we have decided to create a common errors page for this particular reason. For those of you who have not installed MSMQ, go here
The workshop features a set of exercises which increase in complexity.
In this excercise you are required to download the initial solution. You can do this by download the following zip-file: page or clone this repository. Make sure the project compiles and builds like a happy kitty.
Create a new message (in the form of a class). The class should be called NinjaMessage and it should consist of the following auto properties :
// When the message was sent.
public DateTime Sent {get;set;}
// Your hipster ninja message.
public String Message {get;set;}
// Write your ninja name so we can display it on stage.
public String Sender {get;set;}
Your class will also need to implement the interface IMessage from the NServiceBus namespace.
So I made my super awesome message. How do I send it to another machine / endpoint? In order to accomplish this, you will need some sort of application which has access to an instance of the IBus interface. Now, NServiceBus will provide you with a default IoC container out of the box (AutoFac), and it needs to be configured at application startup, in order for you to access NServiceBus in your code. If you prefer to use other containers this is possible (we prefer to use structuremap ourselves, but feel free to use whatever you like).
The steps to complete this exercise are as follows:
- Make a choice if you want to create a console application / web application / wpf, this is up to you. The only thing you need is an instance of the bus.
- Install the nuget package NServiceBus. In case of problems, consult the wiki or close-by nerd.
- NServiceBus needs to be configured at application startup. This is done by using a fluent interface. Configuration for a web project will typically be done from Global.asax.cs . For other project types we expect you to figure out this on your own :)
Here's an example configuration, fluent-style:
Configure.With()
.Log4Net()
.DefaultBuilder() // NServiceBus will now use AutoFac as the IoC container
.XmlSerializer() // Use XML as data format in our messages (recommended)
.MsmqTransport() // Use MSMQ for transport layer. NServiceBus supports other messaging systems as well.
.UnicastBus() // Unicast transmission is the sending of messages to a single destination. Use this :)
.SendOnly(); // In short you would use this in endpoints whose only purpose is sending messages, websites are often a good example of send only endpoints. For example when they receive an order, or some other event / message that should with other endpoints.
So I configured this stuff, how do I send my message? For a web scenario you could accomplish sending a message by creating a controller, injecting an instance of your IBus, and using it to send a message.
public class SendMyMessageController: Controller {
private IBus _bus;
SendMyMessageController(IBus bus) {
_bus = bus;
}
public void Index()
_bus.Send(new NinjaMessage {Sent = DateTime.Now, Sender = "MySecretNinjaName",Message="HelloWorld"});
}
}
If you try to run this piece of code, you will get an exception: No destination specified for message(s): Contracts.NinjaMessage. The next excercise will show you how to set a destination for your messages.
In the previous exercise the destination for the message was not configured. Normally one would do this by configuring the given type of message in the application's configuration file. See example below:
<configSections>
<section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig,NServiceBus.Core" />
</configSections>
<UnicastBusConfig>
<MessageEndpointMappings>
<add Messages="NServiceBusWorkshop.Contracts" Endpoint="NServiceBusWorkshop.Endpoint" />
</MessageEndpointMappings>
</UnicastBusConfig>
The most intersting part is the following:
<add Messages="NServiceBusWorkshop.Contracts" Endpoint="NServiceBusWorkshop.Endpoint" />
We define that we should send messages found in the following namespace: NServiceBusWorkshop.Contracts , and that they should be sent to a queue at your local machine NServiceBusWorkshop.Endpoint. If you want to send this message to a remote computer, you do this by specifying the queue name followed by an @. For example NServiceBusWorkshop.Endpoint@someremotemachine.com.
Create a new project! Name it anything you like, but adding "reciever" or "handler" in the name may be a sound idea :) Define a new class, make it implement IHandleMessages, e.g:
public class NinjaMessageHandler : IHandleMessages<NinjaMessage>
{
public void Handle(NinjaMessage message)
{
Console.WriteLine(message.Sender + " (" + message.Time + "): " + message.Message);
}
}
You've now told NServiceBus that you will handle all incoming NinjaMessage(s) in NinjaMessageHandler. As you see, the content of the message is displayed to console. If you have already installed nuget package "NServiceBus.Host" for the current project, you will notice that EndpointConfig.cs has been added. If you have not installed "NServiceBus.Host", please do so now! This is the "fluent configuration" section that NServiceBus will load at startup.
Now, last thing before you press F5, remember to select multiple startup projects in the Solution Properties in order to run the sender and reciever simultaneously.
Out of the box NServiceBus will attempt to deliver your message 5 times, immediately. In case of some kind of longer lasting error (i.e not a deadlock or some other "short lived" issue), you might want to explore second level retries, commonly abbreviated as SLR in your application. This is quite simple to configure, and only requires some few lines of configuration in app/web.config.
First you need to declare the following configuration section:
<section name="SecondLevelRetriesConfig" type="NServiceBus.Config.SecondLevelRetriesConfig,NServiceBus.Core" />
The implementation might look something like below. In this example NServiceBus will wait 25 seconds before attempting to do a new retry, if this attemp fails, it will wait another 25 seconds before trying again. This will happen up to 100 times before the message is moved to the error queue. It will then be removed from the outgoing queue until someone moves it back.
<SecondLevelRetriesConfig Enabled="true" TimeIncrease="00:00:25" NumberOfRetries="100" />
You have gone too far! We have no more tasks for you (you thought).... For those of you who have managed to get this far throughout these two hours, well done! . You might be curious to explore other usages of NServiceBus, for example publish subscribe. We have provided an example for you to play with....