XmlIncludeAttribute 與 KnownTypeAttribute

來源:互聯網
上載者:User

為了實現一個通用的WCF服務調用的Recovery機制,我們定義了下面的實體類,用以表現當初調用服務時的資訊,包括服務資訊和調用參數。

    public class ServiceInvoikeRecoveryData
{
public string Key { get; set; }

public string ServiceAssembly { get; set; }

public string ServiceType { get; set; }

public string Operation { get; set; }

public object[] Parameters { get; set; }

public string ExceptionMessage { get; set; }

public int FailedCount { get; set; }
}

由於我們是做一個通用的WCF服務調用的Recovery機制,因此調用參數只能定義成object[] Parameters,然而這樣帶來的一個問題就是如果Parameters中的對象有一個不是.NET 原生類型的話在序列化的時候就會出錯。因為我們不知道object[] P

arameters裡面會存什麼類型的Object,所以在序列化的時候如果遇到未知的類型序列化器就會報錯。

這種情況下我們有兩種解決方案,一種是靜態解決方案,通過為類型添加XmlIncludeAttribute的方式來告知序列化器可能的類型有哪些;另一種是在建立XmlSerializer的時候可以告訴序列化器可能的類型有哪些。
   方案一:

    [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method | AttributeTargets.Struct | AttributeTargets.Class, AllowMultiple = true)]
public class XmlIncludeAttribute : Attribute
    [XmlInclude(typeof(OrderData))]
public class ServiceInvoikeRecoveryData
{
public string Key { get; set; }

public string ServiceAssembly { get; set; }

public string ServiceType { get; set; }

public string Operation { get; set; }

public object[] Parameters { get; set; }

public string ExceptionMessage { get; set; }

public int FailedCount { get; set; }
}

 方案二:

public XmlSerializer(Type type, Type[] extraTypes)
XmlSerializer serializer = new XmlSerializer(typeof(ServiceInvoikeRecoveryData),new Type[]{typeof(OrderData)});

以上只是說了使用XmlSerializer方式的解決方案,其實使用DataContractSerializer也有同樣的問題和類似的解決方案:
 方案一:

    [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public sealed class KnownTypeAttribute : Attribute
{
}
    [KnownType(typeof(OrderData))]
[DataContract]
public class ServiceInvoikeRecoveryData
{
[DataMember]
public string Key { get; set; }

[DataMember]
public string ServiceAssembly { get; set; }

[DataMember]
public string ServiceType { get; set; }

[DataMember]
public string Operation { get; set; }

[DataMember]
public object[] Parameters { get; set; }

[DataMember]
public string ExceptionMessage { get; set; }

[DataMember]
public int FailedCount { get; set; }
}

 方案二:

public DataContractSerializer(Type type, IEnumerable<Type> knownTypes);
DataContractSerializer serializer = new DataContractSerializer(typeof(ServiceInvoikeRecoveryData), new Type[] { typeof(OrderData) });

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.