How to use Container instead of ObjectFactory in StructureMap ServiceActivator?

后端 未结 1 747
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 20:40

When implementing DI in WebAPI with StructureMap, we used the ServiceActivator found in

  • Configuring Dependency Injection with ASP.NET WebAPI
1条回答
  •  不知归路
    2020-12-02 20:50

    The static stuff is going away. If your not using a Service Locator of some type you're going to have implement your own "ObjectFactory" as referenced here:

    public static class ObjectFactory
    {
        private static readonly Lazy _containerBuilder =
                new Lazy(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication);
    
        public static IContainer Container
        {
           get { return _containerBuilder.Value; }
        }
    
         private static Container defaultContainer()
         {
            return new Container(x =>
            {
                   // default config
             });
         }
    }
    

    Update: My previous answer was wrong. Thanks @JoeMighty for the heads up.

    0 讨论(0)
提交回复
热议问题