KnownType is not autogenerated in reference.cs

462 Views Asked by At

I have two types defined in my wcf webservice. DynamicFields and ClassA.

[DataContract]
[KnownType(typeof(ClassA[]))]
public class DynamicFields
{
    [DataMember]
    public Dictionary<string, object> properties = new Dictionary<string, object>();

    public object this[string name]
    {
        get
        {
            if (properties.ContainsKey(name))
            {
                return properties[name];
            }
            return null;
        }
        set
        {
            properties[name] = value;
        }
    }
}

[DataContract]
public class ClassA
{
    [DataMember]
    public int Id { get; set; }

    ...
}

And when I add the service as a service reference in my client, I get the System.ServiceModel.Dispatcher.NetDispatcherFaultException

An exception of type

'System.ServiceModel.Dispatcher.NetDispatcherFaultException' occurred in mscorlib.dll but was not handled in user code

Additional information: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:GetResultsResult. The InnerException message was 'Error in line 1 position 3900. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/SomeNamespace:ArrayOfClassA'. 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 'ArrayOfClassA' 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.

I have looked into the reference.cs file auto-generated by the client and found the KnownType attribute is not there

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="DynamicFields", Namespace="http://schemas.datacontract.org/2004/07/SomeNamespace")]
[System.SerializableAttribute()]
public partial class DynamicFields : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

    ...
}

Then I manually add the attribute [KnownType(typeof(ClassA))] in reference.cs and it works

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="DynamicFields", Namespace="http://schemas.datacontract.org/2004/07/SomeNamespace")]
[System.SerializableAttribute()]
[KnownType(typeof(ClassA[]))] //This is what I manually added
public partial class DynamicFields : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

    ...
}

The problem here is every time I update the service reference, I need to go to the reference.cs and manually add the knowntype attribute. Is there any way to make the client auto-genereate it?

0

There are 0 best solutions below