Dispose on object instances getting called twice when disposing the nested container at the end of the httprequest. However, controller dispose called once correctly. Below is detailed info on issue. Kindly advice.
Running Webapi+Owin
Startup.cs var registry = new Registry(); registry.IncludeRegistry(); registry.IncludeRegistry(); var container = new Container(registry); config.Services.Replace(typeof(IHttpControllerActivator), new ServiceActivator(container));
3.Service Activator for 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 Controller Registry Convention
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());
});
}
}
If I use the scan.WithDefaultConventions(), EventStoreHandler Dispose is called once (as expected). However, I do not want to use the WithDefaultConventions. Using anything else calls the dispose twice.