Hi;
I want to return a MobileResult class from my WCF Rest web service on every OperationContract that return value.
I can do this with classes that has no class list inside of it. But when I return a class that has class list inside of it it doesn't serialize the class.
So, how can I return a class that has class list inside of it? Or can I do this?
Thank you.
What I have tried:
My MobileResult class;
[KnownType(typeof(SomeClass))]
[KnownType(typeof(AnotherClass))]
[DataContract]
[Serializable]
public class MobileResult
{
[DataMember]
public bool result { get; set; }
[DataMember]
public object data { get; set; }
public MobileResult(bool res, object obj)
{
result = res;
data = obj;
}
}
My SomeClass, this class can serialize perfectly;
public class SomeClass
{
public int ID { get; set; }
public string Name { get; set; }
}
My AnotherClass, this class doesn't serialize;
public class AnotherClass
{
public int ID { get; set; }
public string Title { get; set; }
public virtual ICollection<SomeClass> Classes{ get; set; }
public AnotherClass{ Classes = new HashSet<SomeClass>(); }
}
Operation Contract;
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetAnotherClass/{number}")]
MobileResult GetAnotherClass(string number);
public MobileResult GetAnotherClass(string number)
{
try
{
AnotherClass aClass = new AnotherClass();
aClass.ID = 5;
aClass.Title = "MyTitle";
aClass.Classes = GetClasses();
return new MobileResult(true, aClass);
}
catch
{
return null;
}
}