I'm having issues with returning an object from a WCF service. A minimal example is included
[ServiceContract]
public interface IService1
{
[OperationContract]
object GetObjectData();
}
When the GetObjectData() method returns a string then everything works fine and my client can read the data, but when it returns a different object for instance, an object in a common assembly:
[DataContract]
[KnownType(typeof(TestObject))]
public class TestObject
{
[DataMember]
public string StringProperty { get; set; }
[DataMember]
public int IntProperty { get; set; }
}
then the client call to the webservice method crashes. From reading around on the internet the KnownType attribute (as above) should have solved all of my issues but I'm now getting the below error from my client. Thanks for any help.
{"The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:GetObjectDataResult. The InnerException message was 'Error in line 1 position 289. Element 'http://tempuri.org/:GetObjectDataResult' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/ClassLibrary1Common:TestObject'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver if you are using DataContractSerializer or add the type corresponding to 'TestObject' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to the serializer.'. Please see InnerException for more details."}