Saturday, February 9, 2013

Beginning NServiceBus

I my day to day work we are using NServiceBus as our Enterprise Service Bus (ESB) implementation in an effort to advance our organization to a form of  service oriented architecture (SOA).  I am a newbie to SOA and NServiceBus, thus the journey has not always been smooth.  I plan on posting short articles about very specific issues that I have encountered with using NServiceBus.  This is the first such article.

I like many developers have to interface to legacy systems.  These systems are usually connected via a .NET assembly and DllImport.  NServiceBus comes with a program called NServiceBus.Host.exe that allows you to host your own service endpoints (basically a message handler).  NServiceBus.Host.exe is provided to make your life easier (and it does).

When using NServiceBus.Host.exe to host your own services, upon loading it will scan the directory for all DLLs, load them and look for implementations of IConfigureThisEndpoint. In certain cases, this may cause NServiceBus.Host.exe to exit with an exception.

  • Multiple implementations of IConfigureThisEndpoint – i.e. multiple endpoints
    • Put each endpoint in its own directory. Besides, you don’t want to couple all of your code together at the deployment level either in SOA.
  • Native DLLs in the directory
    • C++ code anyone? Me too.
  • Probably other reasons that I will soon discover

You can prevent this scanning by including an NServiceBus.Host.exe.config file that points directly to the assembly that implements IConfigureThisEndpoint.  Here is a sample:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="EndpointConfigurationType" value="YourNamespace.YourTypeName, YourAssembly"/>
</appSettings>
</configuration>


NServiceBus.Host.exe will still scan DLLs for other interface implementations during configuration (like IWantCustomInitialization), even with the NServiceBus.Host.execonfig file in place.  So you will still have to exclude specific DLLs from the configuration scanning process.  Here is an example, the Init() method of an IWantCustomInitialization handler:



public void Init()
{
Configure.With(AllAssemblies.Except("legacy.dll").And("native.dll"))
.Log4Net()
.XmlSerializer();
}


You can keep chaining “.And” calls to cover all of the DLLs necessary. I hope this saves you some time and frustration. 



There is a lot of great documentation at http://support.nservicebus.com/