Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity - Resolution of the dependency failed (without registering)

I'm getting an error on this line of code:

    using (IMaterialClient rawMaterialServiceProxy =
ServerUtility.Container.Resolve<IMaterialClient>())

The error:

Resolution of the dependency failed... The current type, Xxx, is an interface and cannot be constructed. Are you missing a type mapping?

I'm not registering a concrete IMaterialClient. In the Pluralsight video I just watched, they said that you don't have to register every type because Unity will find an implementation if one wasn't specified. Has that changed? Am I missing something? Why won't that resolve? The assembly with the actual IMaterialClient implementation is in the bin folder when running this.

like image 240
Bob Horn Avatar asked Dec 21 '25 05:12

Bob Horn


2 Answers

If they said that about Unity, they're wrong. Unity will resolve a concrete type (.Resolve<MyClass>), but interfaces have to be explicitly registered by associating them with concrete types.

There are extensions such as Unity Auto Registration to provide those features; I have no experience with them.

like image 118
TrueWill Avatar answered Dec 22 '25 19:12

TrueWill


I'm not aware of that feature in Unity. As far as I know, it will happily resolve unregistered concrete types, but has to have had a concrete type registered for any abstract types or interfaces. Best bet is to register it:

ServerUtility.Container.RegisterType<IMaterialClient, ConcreteMaterialClient>();
like image 38
Chamila Chulatunga Avatar answered Dec 22 '25 20:12

Chamila Chulatunga