You would need to enable JsConfig.IncludeTypeInfo = true;
on the client side, so the serializer includes the type information with the request. This will add an extra property (__type
) with the type definition so the service knows what to type it as.
It fails currently because requests by default don't provide type information to deserialize the object into the class that implements the interface. This was an issue that was previously raised.
The problem is the when the JSON client makes the request, it will serialize up the a class that implements IThisOrThat
such as your This
class. But when it gets to the other end ServiceStack.Text doesn't know what to deserialize the object into. The type information is lost so it doesn't know what kind of IThisOrThat
it is. So without the additional __type
information property in the request this is happening:
Scenario:
interface ISomething
{
string Name;
}
class MySomething : ISomething
{
public string Name { get; set; }
public int Age { get; set; }
}
class MySomethingElse : ISomething
{
public string Name { get; set; }
public int Size { get; set; }
}
Then you make the call from your JsonServiceClient using a typed object
client.Get(new MySomething { Name: "Duck", Age: 20 });
The JSON that is sent would be { "Name":"Duck", "Age":20 }
what type does the deserialiser choose now? It could be an MySomething
or a MySomethingElse
, or even another ISomething
that it just doesn't know about yet. So because it can't decide the result is simply null
.
Generally interfaces and DTOs don't mix, see here.