Search code examples
c#wcf

WCF: The server did not provide a meaningful reply;


I have a rather trivial WCF service which sends sensor status back and forth.

One of the types contain a property of type Type

public class SensorValue
{
    public string Name { get; set; }
    public string Value { get; set; }
    public Type ValueType { get; set; }
}

When I retrieve the list of sensor values via WCF I get the error mentioned in the subject.

Is there an obvious reason why I am not allowed to (de)serialize Type over WCF?


Solution

  • The System.Type is abstract. Assuming you set that property with for example a typeof(System.Int32) you'll get a concrete implementation, most likely a System.RuntimeType. (Un)fortunately that type is not public so the Datacontract serializer simply can't get its hands on it. It fails with a:

    SerializationException: Type 'System.RuntimeType' with data contract name 'RuntimeType:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.

    It is of no use to try any of that guidance to solve your problem. It is much easier to replace the type of your ValueType property to something sane, like a string for example, and provide enough info so the client can recreate the type based on what you provided in ValueType.

    I used the code in this answer from Richard Petheram to confirm my suspicion that the root cause was a catastrophic failure of the serializer.