2

I am trying to get acquanited with NServiceBus 5 using its own dependency injection container, and with SimpleInjector, which is our container of choice.

There is a way to tell NServiceBus to inject the

IBus Bus {get;set;}

property into all ASP.NET MVC ApiControllers (or somewhere else) using a custom NServiceBusControllerActivator and NServiceBusDependencyResolverAdapter.

However, we want to use SimpleInjector, and don't have an MVC application, and we don't want to inject the Bus into the controllers directly. The controllers should be light-weight, so the logic is inside of "services", which in turn call Bus.Send(message).

How would you accomplish injecting the Bus property using SimpleInjector? I know that people have tried configuring SImpleInjector with NServiceBus but they just don't work together.

John
  • 3,591
  • 8
  • 44
  • 72
  • So you have some IBus, and you want it to be injected into some of your classes. Why is this different from anything you are already doing? Why is it impossible to register that IBus? If I recall correctly the bus is a singleton. – Steven Oct 18 '14 at 08:00
  • @John, not sure I follow the question, but you can let NServiceBus know what container you're using and set a custom container. Here's documentation http://docs.particular.net/nservicebus/containers – Sean Feldman Oct 20 '14 at 05:03
  • @Sean yes other containers are supported but SimpleInjector is not one of them (see link in my question). – John Oct 20 '14 at 09:46
  • Why do you want to inject it as a property instead of using standard constructor injection? – Steven Oct 20 '14 at 13:29
  • @Steven good question. NServiceBus does it via an INeedInitialization interface. Classes implementing that interfacing will get their Bus property injected using one of the supported containers. I haven't used IoC before so forgive my ignorance. Our container is SimpleInjector (fast, easy) which is not supported. – John Oct 20 '14 at 13:58

1 Answers1

0

As you can read here, me and Kijana had tried to come up with a full solution for integrating Simple Injector with NServiceBus. The problem that Kijana describes however (with appending to collections) has been solved for a long time already. However, there are many other problems with integrating a container with NServiceBus, because NServiceBus implements the conforming container anti-pattern.

I'm not sure if this helps you into the right direction, but you can let Simple Injector build-up instances that are created elsewhere and this allows injecting the IBus into properties, as can be read here and here. In short, you can configure Simple Injector to do property injection like this:

class PropertySelectionBehavior<TAttribute> : IPropertySelectionBehavior
    where TAttribute : Attribute {
    public bool SelectProperty(Type type, PropertyInfo prop) {
        return prop.GetCustomAttributes(typeof(TAttribute)).Any();
    }
}

// Usage:
var container = new Container();
container.Options.PropertySelectionBehavior =
    new PropertySelectionBehavior<ImportAttribute>();

container.RegisterSingle<IBus>(bus);

And you can use the following code to build-up stuff:

public static BuildUp(object instance) {
    InstanceProducer producer =
        container.GetRegistration(instance.GetType(), throwOnFailure: true);
    Registration registration = producer.Registration;
    registration.InitializeInstance(instance);
}
Steven
  • 166,672
  • 24
  • 332
  • 435
  • I saw that some work was also done here https://github.com/dotnetjunkie/NServiceBus.SimpleInjector does anyone know DI with NServiceBus Simple Injector now works ? – John Kattenhorn Dec 29 '14 at 15:00
  • @JohnKattenhorn, while building that code, I had a lot of questions about what NSB ansolutely requires, but those answers are still unanswered. So that code might work in some cases, while it might fail in other. Problem is that NSB is build with a specific container in mind. So if you can, let NSB use its default container, and plugin your favorite container while resolving your own types. What the best way to do this however, is unknown to me. – Steven Dec 30 '14 at 02:25