1

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>
BanksySan
  • 27,362
  • 33
  • 117
  • 216
  • Show you config too (small portion relevant to registering the type) – Alexei Levenkov Jan 24 '14 at 16:55
  • @AlexeiLevenkov Done. Cheers. – BanksySan Jan 24 '14 at 17:04
  • @AlexeiLevenkov I suspect I need to declare IBar somewhere, but I con't see how in XML. In code configuration it just works, I don't register IBar anywhere. – BanksySan Jan 24 '14 at 17:05
  • (I have not look into XML config for Unity much, so can't really answer - config looks OK to me). Are you sure if config is even read at all (i.e. does it resolve any other interfaces?) I don't believe you need to register `IBar<..>` (at least in code config you'd not do that). – Alexei Levenkov Jan 24 '14 at 17:15
  • @AlexeiLevenkov I can see that it would have to be aware of that interface, so I can see that i would have to tell it where to look to find out the details of it. – BanksySan Jan 24 '14 at 17:16
  • @AlexeiLevenkov It seems insane that I'd have to declare `IBar` rather than `IBar` in any case. – BanksySan Jan 24 '14 at 17:17
  • What type are you actually resolving? What code results in this error? – Chris Tavares Jan 24 '14 at 22:53

1 Answers1

1

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"
TTat
  • 1,486
  • 8
  • 12
  • If the OP could share the code that reads the configuration, sets up the container, and resolves the object, that would confirm. Based on the current info, however, I expect this answer is correct. – Chris Tavares Jan 24 '14 at 22:54