none
Serialization issue for type DataSet and Dictionary<TKey, TValue> RRS feed

  • Question

  • I'm using Visual Studio 2010 and .NET Framework Runtime 4.0.30319.18052.

    I added service reference to my WCF service. In the generated code I found XmlSerializer instead of DataContractSerializer was chosen for the serialization. This caused some issues which made my project unable to build.

    In my WCF service contract code, type DataSet and Dictionary<TKey, TValue> are used. If I took them out, when adding service reference, DataContractSerializer will be used for generating the proxy code.

    I think this might be a bug of .NET Framework Runtime 4.0.30319.18052. Because in old version .NET Framework Runtime 4.0.30319.296, DataContractSerializer will always be used.

    Can any one provide some suggestions? Or is there a newer version than 4.0.30319.18052 that I can install?

    Thanks,

    Wednesday, October 23, 2013 8:07 PM

Answers

  • Hi,

    In order to let this work you will need to use the KnownType or ServiceKnownType attributes to specify all the types of objects you might put in the dictionary.

    You simply need to add the following property to your datacontract class.

    [DataMember]
    public object UsedForKnownTypeSerializationObject;

    So now the generated proxy contains the Knowtypes you set on the datacontract. If you don't at the a property of type Object to you DataContract class, the generated proxy doesn't contain the declared knowtypes

    For example:

    [DataContract]
    [KnownType(typeof(List<String>))]
    public class Car
    { [DataMember] public String CarName { get; set; } [DataMember] public IDictionary<String, Object> Inputs { get; set; } [DataMember] private Object UsedForKnownTypeSerializationObject{ get; set; } }
    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 25, 2013 5:56 AM
    Moderator

All replies

  • Hi,

    In order to let this work you will need to use the KnownType or ServiceKnownType attributes to specify all the types of objects you might put in the dictionary.

    You simply need to add the following property to your datacontract class.

    [DataMember]
    public object UsedForKnownTypeSerializationObject;

    So now the generated proxy contains the Knowtypes you set on the datacontract. If you don't at the a property of type Object to you DataContract class, the generated proxy doesn't contain the declared knowtypes

    For example:

    [DataContract]
    [KnownType(typeof(List<String>))]
    public class Car
    { [DataMember] public String CarName { get; set; } [DataMember] public IDictionary<String, Object> Inputs { get; set; } [DataMember] private Object UsedForKnownTypeSerializationObject{ get; set; } }
    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 25, 2013 5:56 AM
    Moderator
  • Thanks Amy! The issue caused by using Dictionary<TKey, TValue> has been resolved.

    But I still have issue using DataSet. Could you please give suggestion?

    Thursday, October 31, 2013 2:32 AM