2

I'm trying to make a web service with both a soap and a rest endpoint. But my object has an object property whose type I do not know at compile time. I have created a custom behavior for my rest endpoint that uses Json.NET for serialization because the default DataContractSerializer cannot serialize my dynamic object. This is all working fine.

[DataContract, JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class TestClass1
{
    [DataMember, JsonProperty]
    public string p1 { get; set; }
    [DataMember, JsonProperty]
    public int p2 { get; set; }
}
[DataContract, JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class TestClass2
{
    [DataMember, JsonProperty]
    public DateTime t1 { get; set; }
    [DataMember, JsonProperty]
    public int t2 { get; set; }

}
[DataContract]
public class TestObject : SerializableDynamicObject
{
    [DataMember, JsonProperty]
    public int Id { get; set; }
    [DataMember, JsonProperty]
    public string Name { get; set; }
    [DataMember, JsonProperty]
    public object Obj { get; set; } //DataContractSerializer can't serialize this
}

The problem is on my Soap endpoint that still uses DataContractSerializer. I'm getting an error when I try to run it, because it can't serialize a dynamic object (I assume).

The error I'm getting is this:

An error occurred while receiving the HTTP response to http://localhost:63222/Service1.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

I tried using this method, but to no avail. It still can't serialize my object.

If I remove the object from my TestObject class everything is fine.

Wesley Lomax
  • 2,067
  • 2
  • 20
  • 34
Adrian Buzea
  • 826
  • 2
  • 11
  • 33
  • Did you try using KnownType attribute?? – Viru Sep 24 '15 at 14:12
  • I have tried it, same problem. If I use it to get an object with the Obj=null then it works but if it's a TestClass1 object for instance it doesn't work. – Adrian Buzea Sep 24 '15 at 14:17
  • But when you returned a value, was it the same type of any type declared in KnownType? – Ricardo Pontual Sep 24 '15 at 14:43
  • I did it like the answer here http://stackoverflow.com/questions/7501846/xml-serialize-dynamic-object . I don't want to specify valid types because I don't have any information about the type. It's not like I know it's going to be type X, Y or Z. It can be anything. – Adrian Buzea Sep 24 '15 at 14:51
  • What do you want to see in the XML for an arbitrary type? On the receiving end, how will you know what type to allocate to deserialize the XML? Possibly related: [Using custom DataContractResolver with multiple assemblies](https://stackoverflow.com/questions/32648535/using-custom-datacontractresolver-with-multiple-assemblies/32661615). – dbc Sep 24 '15 at 18:11
  • I'll just assume every type that's gonna be used is going to be serializable by either DataContractSerializer or XmlSmerializer – Adrian Buzea Sep 25 '15 at 09:52

0 Answers0