简体   繁体   English

工厂方法的Autofac单件

[英]Autofac singleton from factory method

I'm pretty much looking for a port to Autofac that is implemented here with Castle Windsor. 我几乎找一个端口Autofac被实施这里有温莎城堡。 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. IBus是EasyNetQ库提供的接口,我想注册一个将IBus实例实例化为单例的工厂。 BusBuilder.CreateMessageBus is the factory method. BusBuilder.CreateMessageBus是工厂方法。 Example in Castle Windsor: Castle Windsor的例子:

container.Register(
    Component.For<IBus>()
             .UsingFactoryMethod(BusBuilder.CreateMessageBus)
             .LifestyleSingleton()
    );

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 第二个解决方案将在Autofac配置期间创建IBus实例,而第二个解决方案将在第一次通过IBus解析IBus时创建它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM