D
D
Dmitry Petrov2012-11-25 19:30:23
.NET
Dmitry Petrov, 2012-11-25 19:30:23

Is it possible to transparently serialize inherited types in WCF?

I am using WebServiceHost to implement a RESTful service. There is a ProxyClass contract, which the server and the client know about, it looks, let's say, like this:

[DataContract]
class ProxyClass
{
 [DataMember]
 public virtual string Property1 { get; set; }

 [DataMember]
 public virtual int Property2 { get; set; }
}

The server has a ProxyClassServer type that looks like this:
class ProxyClassServer: ProxyClass
{
 public override Property1 {
  get { return "Test!"; }
  set { }
 }
}

The client is unaware that the server has its own ProxyClass implementation. The client receives json or xml data from the server and deserializes it into a ProxyClass, assigning appropriate values ​​to all the required fields, and works with this structure.
If the server returned an object of the ProxyClassServer class, then the client must receive an xml structure that specifies what should be deserialized in the ProxyClass. The problem is server side serialization.
In WCF, you can use IDataContractSurrogate. When implementing the interface, we get the method GetDataContractType and GetObjectToSerialize (among others). In principle, in GetObjectToSerialize we get a reference to an object of the ProxyClassServer class and targetType=ProxyClass, but here the problem is that the object must be recreated in order to send the base class object to the serializer. The serializer does not understand if it receives an object of type ProxyClassServer - either swears that the type is not marked as DataContract, or sends an indication to the client that the serialized type is ProxyClassServer
In general, I’ve been tinkering for a long time and I can’t figure out in any way whether it is possible to deceive the serializer and slip it an object with an inherited type without any additional spins with recreating objects. If someone in the habra community has come across this, please tell me, or, if this is generally the wrong approach, then tell me, plz, how to do it right.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Petrov, 2012-11-25
@Sellec

The same situation is described here , but the author solved the problem “on the forehead” - by recreating the object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question