Modules are classes inheriting from IMessageModule. The interface exposes the methods OnStart, OnError and OnFinished which will be called and the beginning, in case of an error and after execution of the message handling chain.

In contrast to message handlers only a single instance of a specific module is created. They are intended to be used to perform custom logic which is not specific to a message. For example an NHibernate session and transaction can be created during OnStart, commited and disposed during OnFinished and rolled back in case of an error.

To create a module, define a class implementing IMessageModule. Like with message handlers, the current working directory is scanned for such classes during the bootstrapping process.

public class GeneralErrorHandlingModule : IMessageModule
  private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(GeneralErrorHandlingModule));

  // called before any message handler of a message are executed.
  public void OnStart()

  // called after executing all message handlers of a message.
  public void OnFinished()

  // may be called before OnFinished if handler throws an exception.
  public void OnError(Exception ex)
    _log.Error("There was an error handling the message.", ex);

Like with message handlers it is possible to have custom dependencies injected to this instance. For example if you need to know the current message have the ILocalContext interface injected. However, the intent of modules is to perform work which needs to be done for each message and is independent from the message type. To perform message depending work it is much more convenient to implement a message handler.

Multiple Modules
If there are multiple modules they are executed one after the other. To have the called in a specific order use the FirstExecuteModule<T>() method during configuration. This defines the which modules OnStart() is called before other ones. OnFinished as well as OnError are called in reverse order.

Last edited Mar 24, 2011 at 6:24 PM by domo42, version 6


No comments yet.