ServiceBehaviorAttribute.IgnoreExtensionDataObject 屬性

定義

取得或設定值,這個值會指定是否要將未知的序列化資料傳送到網路上。

public:
 property bool IgnoreExtensionDataObject { bool get(); void set(bool value); };
public bool IgnoreExtensionDataObject { get; set; }
member this.IgnoreExtensionDataObject : bool with get, set
Public Property IgnoreExtensionDataObject As Boolean

屬性值

Boolean

如果絕不傳送未知的序列化資料,則為 true,否則為 false。 預設為 false

範例

下列範例會示範 IgnoreExtensionDataObject 的使用和 IExtensibleDataObject 的實作。 在這個範例中,藉由將 IgnoreExtensionDataObject 設定為 false,用戶端便會將所知道的額外資料再傳回用戶端。

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(Namespace = "http://microsoft.wcf.documentation")]
  public interface ISampleService{
    [OperationContract]
    Person SampleMethod(Person personParam);
  }

  [DataContract(Name="OriginalPerson", Namespace="http://microsoft.wcf.documentation")]
  public class Person : IExtensibleDataObject
  {
    [DataMember]
    public string firstName;
    [DataMember]
    public string lastName;
    [DataMember]
    public string Message;
    [DataMember]
    public XmlNode[] Blob;

    #region IExtensibleDataObject Members
    private ExtensionDataObject data = null;

    public ExtensionDataObject ExtensionData
    {
      get
      {
        return this.data;
      }
      set
      {
        this.data = value;
      }
    }
    #endregion
  }

  [ServiceBehaviorAttribute(
    IgnoreExtensionDataObject=false,
    ValidateMustUnderstand=false
  )]
  class SampleService : ISampleService
  {
  #region ISampleService Members
    public Person SampleMethod(Person msg)
    {
      Console.WriteLine(msg.firstName);
      Console.WriteLine(msg.lastName);
      Console.WriteLine(msg.Message);

      msg.lastName = "First Name";
      msg.firstName = "Last Name";
      msg.Message = "This is the Reply message.";
        return msg;
    }
  #endregion
  }
}

備註

如果類型實作 IExtensibleDataObject 介面,它會儲存在還原序列化為該類型時,它不知道該資料會透過網路傳送的任何額外資料。 例如,如果型別 Person 具有成員 FirstNameLastName,這時若有個 PhoneNumber 的項目進入,該項目便會被儲存起來。 當稍後序列化此型別時,PhoneNumber 將會重新發出。 問題在於該服務所匯出的架構 Person 只有 FirstNameLastName ,因此Windows Communication Foundation (WCF) 會產生架構不正確實例! 如果嚴格的結構描述相容性很重要,您可以將 IgnoreExtensionDataObject 設定為 true,以便關閉這個重新發出的行為。

不論設定為何 IgnoreExtensionDataObject ,WCF 一律會處理 (傳入和輸出) 的已知資料,而且不會在額外資料出現時擲回例外狀況。 您也可以在應用程式組態檔中使用 < dataContractSerializer > 元素來設定此屬性。

適用於