0

I imagine a lot of people must have the same need as I do, and there should be a really simple solution. Please rid me of this delusion if it is one.

I haven't taken a lot of time to read about the DataContractSerializer, and am hoping I don't really need to... This is in a WCF code-first context and it isn't much I care about with respect to the XML on the wire.

Basically, I have a service-oriented solution with multiple components. I want to expose some things internally (between my services) but not to external callers.

At first I thought I could do something like this:

[DataContract]
//[KnownType(typeof(MyEntityInternal))]
public class MyEntity 
{
    [DataMember]
    public string Toto { get; set; }
    [DataMember]
    public string Tata { get; set; }
}

[DataContract]
//[KnownType(typeof(MyEntity))]
public class MyEntityInternal : MyEntity
{
    [DataMember]
    public DateTime When { get; set; }
}

That is, I didn't think I'd need any "known types" or similar to handle normal inheritance. The idea is to build all the logic so it works only with internal types. Then I can create a service facade for externally exposed services. For output, I shouldn't need to do anything more than declare the external type.

That doesn't work. I get exceptions complaining about unexpected types, and hints about KnownTypes. So I tried adding KnownType attributes (you can uncomment them), and this indeed means I can serialize "internal as external", but annoyingly the serializer then always serializes everything.

I tried this:

string Serialize<T>(object obj)
{
    var xs = new DataContractSerializer(typeof(T));
    var ms = new MemoryStream();
    xs.WriteObject(ms, obj);
    ms.Position = 0;
    var r = new StreamReader(ms);
    return r.ReadToEnd();
}

In my test, "obj" is always an instance of MyEntityInternal (declared as object, if that matters). Then I attempt to Serialize(obj) and Serialize(obj).

As mentioned, this does "work" (ie. doesn't crash!) when I include the KnownType attributes. But "When" is serialized even when I serialize the object "as external".

I do realize that I could create the two types as unrelated (both inheriting from object) and write a mapper, but it seems like a huge waste of both development effort and cycles. Surely there must be some simple solution to this problem??

If you know of a good resource discussing this or closely related matters, please give me a pointer. I've had a quick look at MSDN but didn't immediately find anything useful, and I really don't want to learn about "contract resolvers" or "extensible data contracts" if it isn't absolutely necessary (and to my mind it just shouldn't be).

Odd thing, that the serializer requires me to give it the type I want to serialize, and then it seems to ignore this and use the run-time type of the object instead. ???

The Dag
  • 1,811
  • 16
  • 22
  • Have you tried interfaces? – Jay Jun 20 '13 at 16:16
  • Does this help? [How can I ignore a property when serializing using the DataContractSerializer](http://stackoverflow.com/questions/1791946/how-can-i-ignore-a-property-when-serializing-using-the-datacontractserializer) – Thomas C. G. de Vilhena Jun 20 '13 at 17:07

0 Answers0