Take the community feedback survey now.
                AI OnAI Off
            
        Take the community feedback survey now.
 
                Hi Valdis
I came up with a very simple fix (see below). Going to try an OWIN variant of it.
But still, I would rather have Episerver dispose of the container internally in the InitializationEngine.
public class ContainerDisposalInitialization : IConfigurableModule
{
	public void ConfigureContainer(ServiceConfigurationContext context)
	{
		var container = context.StructureMap();
		if (HostingEnvironment.IsHosted)
		{
			// Keep a reference to the container, and register a custom shutdown handler instance.
			HostingEnvironment.RegisterObject(new DisposeServicesOnShutdownHandler(container));
		}
	}
	
	public void Initialize(InitializationEngine context) { }
	public void Uninitialize(InitializationEngine context) { }
}
public class DisposeServicesOnShutdownHandler : IRegisteredObject
{
	private readonly IContainer _container;
	public DisposeServicesOnShutdownHandler(IContainer container)
	{
		_container = container;
	}
	
	// When IIS is shutting down, this Stop method will be called.
	// This gives us a chance to dispose the StructureMap container.
	// StructureMap then calls Dispose() on singleton services that supports it.
	public void Stop(bool immediate)
	{
		_container?.Dispose();
		
		HostingEnvironment.UnregisterObject(this);
	}
} 
    
    
    
I have a solution in which I spin up a service as a singleton instance. It inherits
IDisposablein order to properly flush and clean up internals. However, theDisposemethod is never called, because the StructureMap container itself is never disposed by Episerver.I attempted to call
IContainer.Disposefrom aIConfigurableModule.Uninitializemethod. But I cannot touch theIContainerin any context other context thanIConfigurableModule.ConfigureContainer.It would be very nice if you could make the
IServiceConfigurationProviderinterface inherit fromIDisposable, and then explicitly dispose it on shutdown. Because then I would be able to remove my custom solution to this problem.