NServiceBus Configuration with Custom Container

1.4k views Asked by At

I am trying to re-use the service registrations in an assembly that I use through a few services in my solution. I follow the example listed from the NServiceBus website to implement the solution. When following that, unless I add the IWantCustomInitialization interface, my Init method (and IoC container implementation) appears not to function. When I have that interface implemented, I get exceptions (listed in SO questions here and here). I can't seem to get it to work that there are no exceptions AND the dependencies in my MessageHandler are being populated properly. Here is my current EndpointConfig implementation.

[EndpointSLA("00:00:30")]
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, UsingTransport<Msmq>, INeedInitialization {
    public void Init() {
        Configure.With().ObjectBuilderAdapter();
    }
}
public class ObjectBuilderAdapter : IContainer {
    readonly IDependencyInjector injector;

    public ObjectBuilderAdapter(IDependencyInjectionBuilder dependencyInjectionBuilder) {
        injector = dependencyInjectionBuilder.Create(); //This method does all the common service registrations that I am trying to re-use
        //injector.RegisterType<ExtractIncomingPrincipal, PrincipalExtractor>();
    }

    public void Dispose() {
        injector.Dispose();
    }

    public object Build(Type typeToBuild) {
        return injector.Resolve(typeToBuild);
    }

    public IContainer BuildChildContainer() {
        return new ObjectBuilderAdapter(new DependencyInjectorBuilder());
    }

    public IEnumerable<object> BuildAll(Type typeToBuild) {
        return injector.ResolveAll(typeToBuild);
    }

    public void Configure(Type component, DependencyLifecycle dependencyLifecycle) {
        injector.RegisterType(component);
    }

    public void Configure<T>(Func<T> component, DependencyLifecycle dependencyLifecycle) {
        injector.RegisterType(component);
    }

    public void ConfigureProperty(Type component, string property, object value) {
        if (injector is AutofacDependencyInjector) {
          ((AutofacDependencyInjector)injector).ConfigureProperty(component, property, value);
        } else {
            Debug.WriteLine("Configuring {0} for property {1} but we don't handle this scenario.", component.Name, property);
        }
    }

    public void RegisterSingleton(Type lookupType, object instance) {
        injector.RegisterInstance(lookupType, instance);
    }

    public bool HasComponent(Type componentType) {
        return injector.IsRegistered(componentType);
    }

    public void Release(object instance) { }
}

public static class Extensions { public static Configure ObjectBuilderAdapter(this Configure config) { ConfigureCommon.With(config, new ObjectBuilderAdapter(new DependencyInjectorBuilder())); return config; } }

Note: When I use the INeedInitialization interface, I get the ComponentNotRegisteredException when it's looking for IStartableBus.

1

There are 1 answers

4
Indu Alagarsamy On

When you are trying to swap the built in container, then you need to implement IWantCustomInitialization in the same class that implements IConfigureThisEndpoint.

You can use your own container and register all your types in there and tell NSB to use that container.

For example:

 public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
    public void Init()
    {
        var container = new ContainerBuilder().Build();
        Configure.With()
            .AutofacBuilder(container);
    }
}