49
votes

I have a WCF service in place.

Normal operation would see the server doing some processing the returning a populated XactTaskIn object to the client via a callback. I have this working ok.

My problem is that when I try and set the returnData variable to a populated XactException and try to send the XactTaskIn back to the client via the callback, I get the following exception thrown.

Exception - "Type 'XactException' with data contract name 'XactException:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver 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 DataContractSerializer." (System.Runtime.Serialization.SerializationException) Exception Message = "Type 'XactException' with data contract name 'XactException:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver 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 DataContractSerializer.", Exception Type = "System.Runtime.Serialization.SerializationException"

Here is the XactTaskIn class

[DataContract]
public class XactTaskIn
{
    [DataMember]
    public DateTime timeOut;
    [DataMember]
    public DateTime timeIn;
    [DataMember]
    public string name;
    [DataMember]
    public string responseTo;
    [DataMember]
    public String moduleFromName;
    [DataMember]
    public String moduleFromType;
    [DataMember]
    public String methodFromName;
    [DataMember]
    public object[] originalInputs;
    [DataMember]
    public String returnMethodToCall;
    [DataMember]
    public String returnModuleToCall;
    [DataMember]
    public object returnData;

    public XactTaskIn(DateTime timeOut, DateTime timeIn, string name, string responseTo, String moduleFromName, String moduleFromType, String methodFromName, object[] originalInputs, String returnMethodToCall, String returnModuleToCall, object returnData)
    {
        this.timeOut = timeOut;
        this.timeIn = timeIn;
        this.name = name;
        this.responseTo = responseTo;
        this.moduleFromName = moduleFromName;
        this.moduleFromType = moduleFromType;
        this.methodFromName = methodFromName;
        this.originalInputs = originalInputs;
        this.returnMethodToCall = returnMethodToCall;
        this.returnModuleToCall = returnModuleToCall;
        this.returnData = returnData;
    }
}

Here is the XactException class:

[DataContract]    
public class XactException
{
    [DataMember]
    string message;

    public XactException(string message)
    {
        this.message = message;
        // Add implementation.
    }
}

Update:

Ok so the comment from Daniel has helped me.

It looks now like the server is sending the callback to the client, but the client is throwing the following exception.

  • Caught: "The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:taskIn. The InnerException message was 'Error in line 1 position 960. Element 'http://schemas.datacontract.org/2004/07/:returnData' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/:XactException'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'XactException' 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 DataContractSerializer.'. Please see InnerException for more details." (System.ServiceModel.Dispatcher.NetDispatcherFaultException) Exception Message = "The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:taskIn. The InnerException message was 'Error in line 1 position 960. Element 'http://schemas.datacontract.org/2004/07/:returnData' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/:XactException'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'XactException' 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 DataContractSerializer.'. Please see InnerException for more details.", Exception Type = "System.ServiceModel.Dispatcher.NetDispatcherFaultException"
3
Try using the KnownTypeAttribute as detailed in the exception.Daniel Hilgarth
@Daniel - Where would I put the attribute?user589195
@user589195: I suppose on the XactException class.Daniel Hilgarth
I tried this at the start of the class [KnownType(typeof(object))]user589195
And what if you use [KnownType(typeof(XactException ))]?Daniel Hilgarth

3 Answers

81
votes

In your class

    [DataContract]
    public class XactTaskIn

you have properties that return objects:

        [DataMember]
        public object[] originalInputs;

        [DataMember]
        public object returnData;

WCF needs to know ahead of time what types can possibly be in there, so that it can tell the client (through the WSDL) what all the types are. For any/all non-'native' types (anything that isnt int, string, DateTime, etc) you will need to add a [KnownType] attribute for every possible type that might be passed back in those object properties, like this:

    [KnownType(typeof(XactException))]
    [KnownType(typeof(...))]
    [KnownType(typeof(...))]
    [DataContract]
    public class XactTaskIn

That way when WCF builds the WSDL for the service, it will know to add XactException to the list of datatypes, and also the serializer will know to look for those classes.


Side-note; if your client was built using SrvUtil, Service Reference, or generated from the WSDL in some way, you will need to rebuild the client after adding the [KnownType] attributes!

0
votes

Your client is expecting a XactTaskIn not a XactException.

You need to change your XactTaskIn to be able to pass an exception object back to your client.

Drop your exception datacontract and add the XactException type to your XactTaskIn class as a data member

0
votes

Seriliazation exceptions occur when the data has changed, for instance something as trivial as changing the name of a field from awesomeString to awesomeSTring will cause serialization to break. The reason for this is the new data can no longer be validated against the old data. The fix for this is use the old version that the server/client is expecting.