views:

242

answers:

1

I'm trying to use StructureMap with nServiceBus.

The Project:

  • Uses a GenericHost Endpoint to send command messages
  • Configures nServiceBus using the StructMapBuilder.
  • Uses a simple StructureMap registry config
  • Uses a start up class TestServer supporting IWantToRunAtStartup
  • The TestServer class has ctor dependency on a TestManager class
  • The TestManager class has ctor dependency on IBus

ObjectFactory.WhatDoIHave() shows StructureMap knows how to construct the classes.

When run I get buildup errors. nServiceBus seems to be overwriting the config?

Note that when I add a IBus ctor depenendency to my event handlers without any other config all appears fine.

Error:

Exception when starting endpoint, error has been logged. Reason: Error creating object with name 'nSeviceBusStructureMapTest.TestServer' : Unsatisfied dependency expressed through constructor argument with index 0 of type [nSeviceBusStructureMapTest.ITestManager] : No unique object of type [nSeviceBusStructureMapTest.ITestManager] is defined : Unsatisfied dependency of type [nSeviceBusStructureMapTest.ITestManager]: expected at least 1 matching object to wire the [miningServiceManage] parameter on the constructor of object [nSeviceBusStructureMapTest.TestServer]

Source:

using System;
using System.Diagnostics;
using NServiceBus;
using StructureMap;
using StructureMap.Configuration.DSL;

namespace nSeviceBusStructureMapTest
{
    public class TestSmRegistry : Registry
    {
        public TestSmRegistry()
        {
            For<ITestManager>().Use<TestManager>();
            For<TestServer>().Use<TestServer>();
        }
    }

    public class TestEndPoint : AsA_Server, IConfigureThisEndpoint
    {
        public void Init()
        {
            Configure.With().StructureMapBuilder(ObjectFactory.Container);
            ObjectFactory.Configure(c => c.AddRegistry<TestSmRegistry>());
            Debug.WriteLine(ObjectFactory.WhatDoIHave());
        }
    }

    public class TestServer : IWantToRunAtStartup
    {
        public TestServer(ITestManager miningServiceManage)
        {
            _miningServiceManage = miningServiceManage;
        }
        private readonly ITestManager _miningServiceManage;
        public void Run()
        {
            _miningServiceManage.Run();
        }
        public void Stop() { }
    }

    public interface ITestManager
    {
        void Run();
    }

    public class TestManager : ITestManager
    {
        public TestManager(IBus bus)
        {
            _bus = bus;
        }
        private readonly IBus _bus;

        public void Run()
        {
            if (_bus == null) Debug.WriteLine("Error no bus");
            // Send messages on bus;
        }
    }
}

  <MsmqTransportConfig InputQueue="test" ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5"  />

  <UnicastBusConfig>
    <MessageEndpointMappings>
    </MessageEndpointMappings>
  </UnicastBusConfig>

Any ideas?

+1  A: 

You have to specify IWantCustomInitialization on the endpoint config class. Otherwise NServiceBus won't call the Init() method. You also need to specify what serializer to use so add:

Configure.With() .StructureMapBuilder() .XmlSerializer();

Hope this helps!

Andreas
NServiceBus will register TestServer for you so you can try to remove the For<TestServer>().Use<TestServer>(); line from the registry as well. Are you using the NServiceBus 2.0?
Andreas
Thanks - That Works.
SimonB