Different unity containers under the same appDomain

Mar 22, 2011 at 9:20 AM

We have sharepoint application. Inside it several asp.net applications are deployed. The goal is to have separate enterprise library configuration per application. The enter point of using EntLib is Unity, so each application will have its own unity container configured using its own enterprise library configuration file. We use Exception Handling, Policy Iinjection, Logging and Interception Application Blocks.

For now I have workable solution: in every application in application start (using httpModule) I create global unity container which is configurated using specific config-file:

private static IUnityContainer CreateContainer()
{
    // create an instance of Unity container
    IUnityContainer unityContainer = new UnityContainer();

    // create the Unity configurator
    IContainerConfigurator configurator = new UnityContainerConfigurator(unityContainer);

    string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "<ApplicationName_EntLib>.config");
    // configure the container
    EnterpriseLibraryContainer.ConfigureContainer(configurator, new FileConfigurationSource(configPath));

    
    // manually create the service locator
    IServiceLocator serviceLocator = new UnityServiceLocator(unityContainer);

    // assign the service locator to Current
    EnterpriseLibraryContainer.Current = serviceLocator;
    

    return unityContainer;
}

But there is a potential problem with the line "EnterpriseLibraryContainer.Current = serviceLocator;".

Because all applications are under the same appDomain this is the common part for them all. But my goal is to separate container instances with their own entLib setting.

Is there any way to go in such a situation? What should I use instead EnterpriseLibraryContainer.Current?

 

Thanks in advance.

(This is the repost from the EntLib forum http://entlib.codeplex.com/discussions/250510)