Problem with registering types in Unity config

Dec 25, 2010 at 8:29 PM

Oh! this is very strange issue. I'm going to register these two types, but there is nothing to be work well.

        <typeAlias alias="IEqualityComparer`1"
                   type="System.Collections.Generic.IEqualityComparer`1, mscorlib" />
        <typeAlias alias="singleton"
                   type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />            
        <typeAlias alias="EqualityComparer`1"
                   type="System.Collections.Generic.EqualityComparer`1, mscorlib" />
                <register type="IEqualityComparer`1"
                    <lifetime type="singleton" />

And this is my controller:

public class MyController : MyExtendedController {
    private readonly IEqualityComparer<int> _fakeComparer;

    public ResourcesController(IEqualityComparer<int> fakeComparer) {
        _fakeComparer = fakeComparer;

And exception throws by Unity when resolving Controller:

Resolution of the dependency failed, type = "MyController", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type EqualityComparer`1 cannot be constructed. You must configure the container to supply this value.
At the time of the exception, the container was:
Resolving MyController,(none)
Resolving parameter "fakeComparer" of constructor   MyController(System.Collections.Generic.IEqualityComparer`1[[int, mscorelib]] fakeComparer)
Resolving System.Collections.Generic.EqualityComparer`1[int, mscorelib],(none) (mapped from System.Collections.Generic.IEqualityComparer`1[int, mscorelib], (none))
Any suggestions would be appreciated ;)

Dec 26, 2010 at 1:12 AM

You need to use the full assembly name when referencing mscorlib - you have to include version, culture, and public key tokens as well. Otherwise the CLR loader won't find it (full names are required when loading from the GAC). Try putting the full names in, not just ",mscorlib" and see if that helps.


Also, there's a better way to do this. If you add:

<namespace name="System.Collections.Generic" />

to the top of your <unity> block and remove the aliases, that should work too (mscorlib is automatically added as an <assembly> in the config file).


Dec 26, 2010 at 6:55 AM

Thanks ctavares. I putted  simplified version of my alias definition. However I also checked your two ways and still there isn't any success!

<typeAlias alias="EqualityComparer`1"
                       type="System.Collections.Generic.EqualityComparer`1, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

I checked these registering/resolving with my own generic Interface/Class and everything works well. But i can't figure it out please help me ;)

Dec 26, 2010 at 4:53 PM

Can anyone create a sample project and register this types through config file and send me that? I'm a novice in Unity.

Dec 27, 2010 at 8:05 PM

Answer are here mvc 3 - Problem with registering types in Unity config - Stack Overflow