Attempt to bind struct to interface throws "The type .. cannot be constructed. You must configure the container to supply this value"

Aug 12, 2011 at 6:58 AM

I have next code:

public interface ISomething { }

public struct ConcreteSomething : ISomething { }

var container = new UnityContainer();
container.RegisterType(typeof(ISomething), typeof(ConcreteSomething));
var s = container.Resolve<ISomething>();

and it throws:

Resolution of the dependency failed, type = "ConsoleApplication1.ISomething", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type ConcreteSomething cannot be constructed. You must configure the container to supply this value.

So it it possible to bind a struct to an interface in Unity?

Aug 12, 2011 at 8:35 AM
Edited Aug 12, 2011 at 8:39 AM

I assume Unity only supports class binding as according to Registering an Interface or Class Mapping to a Concrete Type:

"This is the most common scenario for dependency injection using Unity. It involves registering a mapping between types such as an interface or a base class and a corresponding concrete class that implements or inherits from it."

 

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

Marked as answer by abatishchev on 12/1/2014 at 10:29 AM