使用嵌套容器时,结构图调用多次处理

时间:2015-10-29 16:46:12

标签: structuremap

当在httprequest的末尾处理嵌套容器时,处理对象实例被调用两次。但是,控制器配置一次正确调用。以下是有关问题的详细信息。请建议。

  1. 运行Webapi + Owin

  2. Startup.cs  var registry = new Registry();  registry.IncludeRegistry();  registry.IncludeRegistry();  var container = new Container(registry);  config.Services.Replace(typeof(IHttpControllerActivator),新的ServiceActivator(容器));

  3. 3. WebAPI的服务激活器

    public class ServiceActivator : IHttpControllerActivator
     {
         private readonly IContainer _container;
         public ServiceActivator(IContainer structureMapContainer)
         {
            _container = structureMapContainer;
         }
    
         public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
         {
            var nested = _container.GetNestedContainer();
            var instance = nested.GetInstance(controllerType) as IHttpController;
            request.RegisterForDispose(nested);
            return instance;
          }
       }
    

    4控制器注册管理机构公约

     public class ControllerConvention : IRegistrationConvention
     {
        public void Process(Type type, Registry registry)
        {
            if (type.CanBeCastTo(typeof(ApiController)) && !type.IsAbstract)
            {
                registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
            }
        }
     }
    

    5.Repository Registry Convention()

     public class RepositoryConvention : IRegistrationConvention
     {
        public void Process(Type type, Registry registry)
        {
            if (!type.IsAbstract)
            {
                registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
            }
        }
    }
    

    6。 RepositoryRegistry

    public class RepositoryRegistry : Registry
       {
        public RepositoryRegistry()
        {
            Scan(scan =>
            {
               scan.TheCallingAssembly();
    
    
               //DISPOSE ON EventStoreHandler GETTING CALLED TWICE AT THE END OF  THE REQUEST
               For<IEventStoreHandler>().Add<EventStoreHandler>().Named("Default");
               For<IEventStoreHandler>().Add<EventStoreHandlerB>().Named("CustomerB");
    
     //scan.WithDefaultConventions();  //DISPOSE ON EventStoreHandler GETTING  CALLED ONCE  AS EXPECTED AT THE END OF THE REQUEST
               For<IEventStoreHandler>().Use("Default"); //Default
    
                scan.With(new RepositoryConvention());
            });
        }
      }
    

    如果我使用scan.WithDefaultConventions(),则调用EventStoreHandler Dispose一次(如预期的那样)。但是,我不想使用WithDefaultConventions。使用其他任何东西都会调用dispose两次。

1 个答案:

答案 0 :(得分:0)

解决方案:

For<IEventStoreHandler>().Add<EventStoreHandlerB>().Named("CustomerB");
For<IEventStoreHandler>().Add<EventStoreHandler>().Named("Default");  

//This line fixed it       
For<IEventStoreHandler>().Use<EventStoreHandler>().Ctor<IEventStoreHandler>().Is(i => i.GetInstance<IEventStoreHandler>("Default"));