Configure container without loading assemblies?

May 27, 2009 at 7:00 PM

We have a common assembly which provides the ability to resolve, instantiate and manage the lifetime of services given an interface.  We are using Unity to map the interface type to the implementation and have all the mappings stored a single container in a configuration file associated with the assembly.  We have discovered that when we use the Configure method of the UnityContainerElement to configure the container, all the assemblies involved in the mappings get loaded immediately.

This is problematic for us because not necessarily all the assemblies will be used at run time resulting in unused assemblies loaded in our process, and it's even possible that some of them may not be present.  Currently, if an assemby is not present an exception is throw while the container is configured.

Is there some way to avoid loading the assemblies until a call to Resolve is made?  That way if the type is never used, the assembly would never be loaded. 

If not, is there any way to skip mappings that involve assemblies which are not present?

Jun 3, 2009 at 5:17 PM


At this point I am thinking of creating multiple containers, with names matching the assemblies of the mapped interfaces.  I.e. Given an interface, I'll determine the name of the assembly that implements it and then load just the container with that name.  This way I don't end up trying to load all the mappings even if I won't be using them thus avoiding problems if assemblies that won't be used anyway aren't present.

Oct 28, 2009 at 6:45 PM

Unity doesn't do anything special about assembly / type loading. We're just relying on the CLR for this. Your best bet is the separate container definitions for each assembly. You can then apply all the appropriate configurations to the same underlying container.