c# - ServiceStack: Property in request DTO becomes null if type is abstract -
i have servicestack 3-based client-server architecture. i'm trying create service request dto contains property abstract type, 2 different concrete classes implementing it. abstract type either abstract class or interface; however, in either case, server receives null object in property.
there's 3 assemblies , corresponding namespaces: testclient
, server
, , commonlib
referenced both client , server.
that is, spread across 3 assemblies:
namespace commonlib.services { public class getthing : ireturn<getthingresponse> // request dto { public ithisorthat context { get; set; } } public class getthingresponse { public dictionary<int, string> result { get; private set; } public getthingresponse(dictionary<int, string> result) // response dto { result = result; } } } namespace commonlib { public interface ithisorthat { } public class : ithisorthat { } // , forth } namespace server.services { public class getthing service : iservice { public object get(getthing request) { var foo = request.context; // null } } } namespace testclient { class program { public const string wsurl = "http://localhost:61435/"; static void main(string[] args) { using (var client = new jsonserviceclient(wsurl)) { var result = client.get(new getthing { context = new commonlib.this("context info") }); } }
if change context
property in getthing
of type this
instead of ithisorthat
, works. leaving interface, or changing ithisorthat
abstract class, results in data being transmitted null
.
i'm assuming serialization problem. i've tried changing interface abstract class , decorating appropriate knowntype
attributes, servicestack's serializer doesn't appear benefit this. there trick done?
you need enable jsconfig.includetypeinfo = true;
on client side, serializer includes type information request. add property (__type
) type definition service knows type as.
it fails because requests default don't provide type information deserialize object class implements interface. issue previously raised.
the problem when json client makes request, serialize class implements ithisorthat
such this
class. when gets other end servicestack.text doesn't know deserialize object into. type information lost doesn't know kind of ithisorthat
is. without additional __type
information property in request 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 make call jsonserviceclient using typed object
client.get(new mysomething { name: "duck", age: 20 });
the json sent { "name":"duck", "age":20 }
type deserialiser choose now? mysomething
or mysomethingelse
, or isomething
doesn't know yet. because can't decide result null
.
generally interfaces , dtos don't mix, see here.
Comments
Post a Comment