3

I'm pretty much looking for a port to Autofac that is implemented here with Castle Windsor. The IBus is an interface provided by EasyNetQ library and I'd like to register a factory that would instantiate the IBus instance as a singleton. BusBuilder.CreateMessageBus is the factory method. Example in Castle Windsor:

container.Register(
    Component.For<IBus>()
             .UsingFactoryMethod(BusBuilder.CreateMessageBus)
             .LifestyleSingleton()
    );
Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
the berserker
  • 1,553
  • 3
  • 22
  • 39

1 Answers1

8

Try this

container.Register(c => BusBuilder.CreateMessageBus())
         .As<IBus>()
         .SingleInstance(); 

or

container.RegisterInstance(BusBuilder.CreateMessageBus())
         .As<IBus>();

The second solution will create the IBus instance during Autofac configuration whereas the second solution will create it the first time an IBus is resolved by Autofac

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62