Skip to content

Instantly share code, notes, and snippets.

@DrivenLogic
Last active December 15, 2015 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DrivenLogic/5297446 to your computer and use it in GitHub Desktop.
Save DrivenLogic/5297446 to your computer and use it in GitHub Desktop.
NSB Bootstraper example, with autofac scanning.
public static void InitNsbHost()
{
string[] assemblyScanerPattern = new[] {@"your.namespace.*.dll"};
// Make sure process paths are sane...
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
// begin setup of autofac >>
ContainerBuilder builder = new ContainerBuilder();
// 1. Scan for assemblies containing autofac modules in the bin folder
List<Assembly> assemblies = new List<Assembly>();
assemblies.AddRange(
Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "*.dll", SearchOption.AllDirectories)
.Where(filename => assemblyScanerPattern.Any(pattern => Regex.IsMatch(filename, pattern)))
.Select(Assembly.LoadFrom)
);
// 2. Register AutoFac modules
assemblies
.SelectMany(assembly => assembly.GetTypes())
.Where(type => typeof (Autofac.Module).IsAssignableFrom(type))
.Where(type => type.GetConstructor(Type.EmptyTypes) != null)
.Select(Activator.CreateInstance)
.Cast<IModule>()
.ToList()
.ForEach(builder.RegisterModule);
// 3. we have all our stuff in the builder now so lets build it
Autofac.IContainer container = builder.Build();
// 4. pass our AutoFac container to NSB for management.
Configure.With()
.AutofacBuilder(container)
.DefiningMessagesAs(t => t.Namespace != null && t.Namespace.EndsWith("yourMessageNamespace")) // aka Unobtrusive mode
.UseNHibernateTimeoutPersister()
.MsmqTransport()
.IsTransactional(true)
.DisableRavenInstall()
.IsolationLevel(IsolationLevel.ReadCommitted) // safe but not nuts
.PurgeOnStartup(false)
.UnicastBus()
.LoadMessageHandlers()
.ImpersonateSender(false)
.DoNotAutoSubscribe()
.CreateBus()
.Start(() => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install()); // thunderbirds are go!
Alarms.WriteInfoEventLog(200, "NService Bus initialise complete");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment