Different unity containers under the same appDomain

Topics: General discussion
Mar 21, 2011 at 1:01 PM

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.

Mar 22, 2011 at 7:27 AM

How many asp.net applications do you have in the sharepoint app?  Is it feasible to separate them in different application pools?  I would also suggest you to post this in the Unity forum.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Mar 22, 2011 at 10:18 AM

There are 2 asp.net applications under sharepoint. Due to our legacy design it is not possible to separate them in different application pools.