Injecting NServiceBus in Orchard

Topics: Customizing Orchard
Mar 5, 2012 at 5:25 AM

I am trying to integrate NServiceBus with Orchard.

In Application_Start of Orchard.Web, I have added

Bus = NServiceBus.Configure.WithWeb()
                .Log4Net()
                .DefaultBuilder()
                .XmlSerializer()
                .MsmqTransport()
                    .IsTransactional(false)
                    .PurgeOnStartup(false)
                .UnicastBus()
                    .ImpersonateSender(false)
                .CreateBus()
                .Start();

I would like to inject this instance of the bus to the orchard host container.  Has anyone tried this or can offer suggestion on what I need to do?  Should I register it in OrchardStarter?

 

 

 

 

 

Developer
Mar 5, 2012 at 6:00 AM

It's pretty easy - just create your own class extending Autofac.Module, add it to your module and register your instance in the overridden Load method. 

    public class MyModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            var myObject = /* initialization code */;
            builder.RegisterInstance(myObject).As<IMyInterface>();
        }
    }

Also please look in the Orchard source - there are lots of examples.

Orchard automatically grabs all implementations of an Autofac modules it finds and registeres them in the container. It's the easiest way to plug in directly into the DI container to register your custom stuff.

Developer
Mar 5, 2012 at 8:27 PM

Awesome, I didn't know that!

 

Mar 5, 2012 at 10:02 PM

@pszmyd - This is exactly what I am after.  Thanks.

Mar 13, 2012 at 5:38 AM
pszmyd wrote:

It's pretty easy - just create your own class extending Autofac.Module, add it to your module and register your instance in the overridden Load method. 

 

    public class MyModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            var myObject = /* initialization code */;
            builder.RegisterInstance(myObject).As<IMyInterface>();
        }
    }

 

Also please look in the Orchard source - there are lots of examples.

Orchard automatically grabs all implementations of an Autofac modules it finds and registeres them in the container. It's the easiest way to plug in directly into the DI container to register your custom stuff.

I have recently ran into some issues using this approach using Orchard 1.4.  The Load method is invoked multiple times (when I enable this module from a fresh install of Orchard, activating a theme, etc).  I only want the nservicebus to be instantiated once.  Is there a way around this?

Coordinator
Mar 13, 2012 at 5:52 AM

Please file a bug if you think this is in error.

Developer
Mar 13, 2012 at 1:02 PM

@QAmelie Is this causing trouble? Module will get loaded each time app reloads, what happens when things you mentioned are being done (setup, activating a theme or module). During normal use it should get fired only once (at startup) - if not, then please file a bug as Bertrand stated.

Jun 14, 2012 at 6:14 PM
Edited Jun 14, 2012 at 6:15 PM

I too would like to use NServiceBus in an Orchard module.  Reading this post, I see that @QAmelie has resorted to adding a line to the global.asax. I'm seeking to find a way that doesn't involve editing Orchard source code, but it seems that NServiceBus's configure method won't work unless it happens during the application startup process.

Is there any way to get code to run during this time in Orchard that doesn't require editing the Global.asax file?

Jun 14, 2012 at 6:52 PM
Edited Jun 15, 2012 at 5:15 AM

While I've found the Configure.With method to be very finicky in its placement, I've been able to get NServiceBus to work without injection using a simple singleton pattern:

using NServiceBus;

public static class BusProxy
{
        public static IBus Bus
        {
            get
            {
                if (_Bus == null)
                {
                        _Bus = Configure.With(new[] { typeof(AuditMessage).Assembly })
                            .Log4Net()
                            .DefaultBuilder()
                            .XmlSerializer("http://www.test.com")
                            .MsmqTransport()
                            .IsTransactional(false)
                            .PurgeOnStartup(false)
                            .UnicastBus()
                            .ImpersonateSender(false)
                            .LoadMessageHandlers()
                            .CreateBus()
                            .Start();
                }

                return _Bus;
            }
        }
        private static IBus _Bus;
}

public class AuditService : IAuditService
{
        public void LogEvent(string message)
        {
            BusProxy.Bus.Send(message);
        }
}