2011-12-16 46 views
3

我很反感,我還沒有找到答案的反序列化問題。該場景如下:如何反序列化不知道的類型,而不是普通的情況

silverlight客戶端登錄到服務器並獲取會話對象。會話對象包含各種信息,其中包括一個名爲UserSettings的集合。該集合包含一個名爲propertyentity的類型,它基本上有兩個屬性,一個字符串和一個對象。反序列化時,我得到了以下錯誤:

System.Runtime.Serialization.SerializationException: JSON contains a '__type' member specifying the data contract name 'http://www.xyz.se/rc:ClientDifferenceRules'. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'ClientDifferenceRules' 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. You can also often eliminate this error by avoiding the use of derived types where the JSON is produced.

當含有例如普通INT作爲反序列化的作品完美session.UserSettings發送其它propretyentities。我怎樣才能告訴解串器它是什麼數據類型?

ClientDifferenceRules看起來是這樣的:

using System; 
using System.Diagnostics; 
using System.Text; 
using System.ComponentModel; 
using System.Runtime.Serialization; 

    namespace XYZ.BusinessEntities.Settings 
    { 

    [Serializable] 
    [DataContract(Name = "ClientDifferenceRules", Namespace = Constants.Namespace)] 
    [KnownType(typeof(ClientDifferenceRules))] 
    public class ClientDifferenceRules : RCBusinessBase 
    { 
     //private fields omitted for better readability 

     public ClientDifferenceRules() : base() 
     { 
     } 

     [DataMember] 
     public override int Id 
     { 
      get { return _levelId;} 
      set { } 
     } 

     [DataMember] 
     public int clientId 
     { 
      get { return _clientId;} 
      set { _clientId = value;} 
     } 

     [DataMember] 
     public int typeId 
     { 
      get { return _typeId; } 
      set { _typeId = value; } 
     } 

     [DataMember] 
     public short levelId 
     { 
      get { return _levelId; } 
      set { _levelId = value; } 
     } 

     [DataMember] 
     public double? limitFrom 
     { 
      get { return _limitFrom; } 
      set { _limitFrom = value; } 
     } 

     [DataMember] 
     public double? limitTo 
     { 
      get { return _limitTo; } 
      set { _limitTo = value; } 
     } 

     [DataMember] 
     public string message 
     { 
      get { return _message; } 
      set { _message = value; } 
     } 

     [DataMember] 
     public string description 
     { 
      get { return _description; } 
      set { _description = value; } 
     } 

     [DataMember] 
     public string backColor 
     { 
      get { return _backColor; } 
      set { _backColor = value; } 
     } 

     [DataMember] 
     public string foreColor 
     { 
      get { return _foreColor; } 
      set { _foreColor = value; } 
     } 

     [DataMember] 
     public string imageUri 
     { 
      get { return _imageUri; } 
      set { _imageUri = value; } 
     } 


     [DataMember] 
     public DifferenceType differenceType 
     { 
      get 
      { 
       DifferenceType enmType = (DifferenceType)this.typeId; 
       return enmType; 
      } 
      set { this.typeId = (int)value; } 

     } 

     [DataMember] 
     public DifferenceLevel differenceLevel 
     { 
      get 
      { 
       DifferenceLevel enmLevel = (DifferenceLevel)this.levelId; 
       return enmLevel; 
      } 
      set { this.levelId = (short)value; } 
     } 

    } 
} 

ClientDifferenceRulesCollection看起來是這樣的:

using System; 
using System.Text; 
using System.Collections.Generic; 
using Momentum.Common.Framework; 
using System.Runtime.Serialization; 

namespace XYZ.RC.BusinessEntities.Settings 
{ 
    [Serializable] 
    [CollectionDataContract(Name = "ClientDifferenceRulesCollection", Namespace = Constants.Namespace)] 
    [TypedCollection(typeof(ClientDifferenceRules))] 
    [KnownType(typeof(ClientDifferenceRulesCollection))] 
    public class ClientDifferenceRulesCollection : BusinessCollectionBase<ClientDifferenceRules> 
    { 
     public ClientDifferenceRulesCollection() : base(){}  
     public ClientDifferenceRulesCollection(UserSession Session) : base(Session){}  
     public ClientDifferenceRulesCollection(IList<ClientDifferenceRules> initialList) : base(initialList) { } 
    } 
} 

PropertyEntity看起來像這樣(從元數據):

[DataContract(Name = "PropertyEntity", Namespace = "http://www.momentum.se//common")] 
public class PropertyEntity 
{ 
    public PropertyEntity(); 
    public PropertyEntity(string name, object value); 

    [DataMember] 
    public string Name { get; set; } 
    [DataMember] 
    public object Tag { get; set; } 
    public TypeCode typeCode { get; set; } 
    [DataMember] 
    public TypeOfProperty TypeOfProperty { get; set; } 
    [DataMember] 
    public object Value { get; set; } 
} 
+0

知識類型通常在基類(或其集合)上進行多態序列化。您可能需要向RCBusinessBase和/或任何集合添加[KnownType(typeof(ClientDifferenceRules))]。您也可以在Web配置中添加已知類型,並且似乎聲明性地向您的服務合同提供 - http://stackoverflow.com/questions/771560/how-do-you-configure-wcf-known-types-programmatically – StuartLC 2011-12-16 09:18:05

回答

0

我放棄了試圖解決這個問題與未知類型的反序列化並重新設計了應用程序而不是。看起來它只是自己設計的實體和集合的層次太多,以至於序列化/反序列化機制無法處理。

現在我通過單獨的WCF服務在集合中獲取類型爲X的實體,它的工作原理類似於魅力。

無論如何感謝您的努力!

相關問題