Problem with IUnityContainer.Resolve when the mapping type is loaded dynamically from library

Aug 13, 2011 at 12:06 PM


some words about task. There is C# Class Library which consists of interfaces, and every interface has single implementation (class) in another C# Class Library. All these libraries compiles in separate assemblies.


//in interface library
public interface IRandom
    int GetRandom();
//in implementation library
using InterphaceLibrary;

public class Random:IRandom
    public int GetRandom()
        var rand = new System.Random();
        return rand.Next();

Mappings between interfaces and implementations stored in XML. And special class Resolver which exists in yet another library performs bindings via MS Unity.
//in resolver library
using Microsoft.Practicies.Unity;
public class Resolver
    private readonly IUnityContainer _container;
    public Resolver()
         _container = new UnityContainer();
    public RegisterAll()
     //parsing XML, getting type, interfaces and assemblies names
     //all names saved in mappings variable   
     //mappings type consists of simple members that are not defined here (AssemblyName, TypeName, ...)
         foreach (var mapping in mappings)
             var assembly = Assembly.LoadFrom(mapping.AssemblyName);
             var iface = mapping.InterfaceName;
             Type interfaceType = assembly.GetType(iface);
             var moduleassembly = Assembly.LoadFrom(mapping.Mapping.AssemblyName);
             var module = mapping.Mapping.TypeName;
             Type moduleType = moduleassembly.GetType(module);
             _container.RegisterType(interfaceType, moduleType);
        // this code works fine and all types and interfaces successfully registered
       // in _container instance
    public object Resolve(Type type)
        return _container.Resolve(type);
And there is Console Application which references to interfaces and resolver libraries. It knows only about interfaces and it use Resolver class instance for getting appropriate implementation.
using ResolverLibrary;
using InterfaceLibrary
class Program
    static void Main(string[] args)
        var resolver = new Resolver();            
        IRandom random = (IRandom) resolver.Resolve(typeof (IRandom));
At runtime i've got unity container error:

"Resolution of the dependency failed, type = "IRandom", name = "(none)". Exception occurred while: while resolving. 
Exception is: InvalidOperationException - The current type, IRandom, is an interface and cannot be constructed. Are you missing a type mapping?"

I am sure that IRandom has mapping. I've checked it in debugger.
The only reason why this error can appear is difference between IRandom type instances. One I get from dynamically loaded library (Assembly.LoadFrom()), another I get statically using typeof (IRandom).
Also I've compared these two type instances in debugger. Most of members including FullName, Assembly info and GUID are equal.
Is it possible to solve this problem using Unity? Thanks.



Aug 16, 2011 at 6:57 AM


I tried the code and yes, I got the same error. According to Chris Tavares on this thread which looks like has the same scenario as yours:

"Are you dynamically loading via Assembly.LoadFrom? If that's the case, you may be running into the "LoadFrom context". This results in two different copies of the same type in your appdomain, and the CLR doesn't consider them the same."

Looking at your code, you registered a dynamically loaded IRandom type, yet in your resolve, you used the referenced IRandom type. Hope this helps :)


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us