简体   繁体   中英

Unity with interface that extends another interface

I have a class:

public class Foo : IFoo
{
}

and two interfaces:

Public interface IFoo : IBar<SomeType>
{
}

public interface IBar<T>
{
    DoSomething(T t);
}

We are using XML Config,the code configuration works fine, but the web.config doesn't.

The error is like:

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

mlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="IFoo"
        type="IFoo, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e4bf38905880b60b" />

    <alias alias="Foo"
        type="Foo, Something, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e4bf38905880b60b" />

    <container name="SomeName">
        <register type="IFoo" mapTo="Foo" />
    </container>
</unity>

I believe you're probably using the default instance of the UnityContainer since you didn't mention anything about using a named instance of the container.

<container name="SomeName">
    <register type="IFoo" mapTo="Foo" />
</container>

You're registering the mapping to "SomeName" instead of the default container.

Remove

name="SomeName"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM