I'm trying to bind a controller action to an interface but still maintain the default binding behavior.
public class CoolClass : ISomeInterface
{
public DoSomething {get;set;} // ISomeInterface
}
public class DosomethingController : ApiController
{
public HttpResponseMessage Post(ISomeInterface model)
{
// do something with model which should be an instance of CoolClass
}
}
The consumer of my service knows nothing of CoolClass so having them add "$type" to the Json they are passing would be a hack in my opinion. I'd like to be able to handle it in the service. If I specify CoolClass as the action parameter it works fine.
EDIT: So I found a partial solution to my question here Dependency injection for ASP.NET Web API action method parameters but there is a follow up issue. That solution does not resolve interface properties. See my example below.
IConcreteClass will be resolved, but ISubtype will not.
public class SubConcreteClass : ISubtype
{
// properties
}
public class ConcreteClass : IConcreteClass
{
public ISubtype Subtype {get;set;}
}
Once the media formatter sees that is can resolve the type in IConcreteClass, it then reads the entire stream. So I'm guessing there is no chance to resolve interface members.