I’ve come onto a problem which I can’t find a good solution to – I’ve got a WCF service, where I want to return an object of ChildClass which inherits from FatherClass.
Mostly, I’d return the ChildClass, but in some cases I’d like to return just the FatherClass (which holds just 1 field “error”).
Can this be accomplished?
My Code:
[WebGet(UriTemplate = "SomeQueryString", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
public ChildClass GetCollection(parameter)
{
if (err)
{
return new FatherClass();
}
else
{
return new ChildClass();
}
}
Where as ChildClass inherits from FatherClass (has less fields).
My objective is to return only a very small fraction of the “text” instead of the text that will be returned if I return the whole ChildClass object.
Ideas? 🙂
Thanks!
That is possible only if you redefine your operation and contracts – you must return parent and serializer must know about all children which can be used instead of the parent:
And your operation will look like: