0
votes

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."}

1
Don't you decorate your Service Contract with KnownType - not your Data Object ?PhillipH
That's not how I've seen it used before. Have got working code elsewhere that returns an object from a common library with the KnownType on the DataContract class which is why I'm confused.C. Knight
Just tried it and I'm getting intellisense saying that you can't add KnownType to an interface. So you can't define it on the ServiceContract.C. Knight
2 questions. Do you have to use WCF (over let's say WebAPI)? Is returning the object as a JSON-string acceptable?smoksnes
Can't really move away from WCF due to the time frame (webservice and client are pretty much coded) and only discovered this issue when starting to deploy them (all my unit tests passed because they were calling the methods in the code rather than the webservice itself). My fall back option is to decorate the classes (in the common code) as serialiseable and return the information as a string...C. Knight

1 Answers

2
votes

From https://msdn.microsoft.com/en-us/library/ms730167(v=vs.110).aspx

You can certainly return Object and use KnownType to define its concrete type but only if Object is a property of a concrete type being returned. You must have a wrapper type containing the object.

KnownType does not apply to the object returned from the contract call, but applies to unknowable types within the type returned from the contract call. Wrapper your Object return value into a concrete type called ... well possibly "GetObjectDataWrapper" with an object property. Decorate GetObjectDataWrapper with the KnownType.