InvalidDataContractException 類別

定義

DataContractSerializerNetDataContractSerializer 在序列化 (Serialization) 和還原序列化 (Deserialization) 期間遇到無效的資料合約時,所擲回的例外狀況。

public ref class InvalidDataContractException : Exception
public class InvalidDataContractException : Exception
[System.Serializable]
public class InvalidDataContractException : Exception
type InvalidDataContractException = class
    inherit Exception
[<System.Serializable>]
type InvalidDataContractException = class
    inherit Exception
Public Class InvalidDataContractException
Inherits Exception
繼承
InvalidDataContractException
屬性

範例

下列範例會示範包含 ICalculatorAdd 兩種方法的 MoreData 服務合約。 MoreData 方法會傳回 ExtraData 類別的執行個體,這個類別也已經宣告在程式碼中。 當實作的介面 Calculator 執行時,因為 InvalidDataContractException 類別沒有套用 ExtraData,所以將會擲回 DataContractAttribute

public  class Test
{
    static void Main()
    {
        try
        {
            Test t = new Test();
            t.Run();
        }

        // Catch the InvalidDataContractException here.
        catch(InvalidDataContractException iExc)
        {
            Console.WriteLine("You have an invalid data contract: ");
            Console.WriteLine(iExc.Message);
            Console.ReadLine();
        }
          catch (Exception exc)
        {
            Console.WriteLine(exc.Message);
            Console.WriteLine(exc.ToString() );
            Console.ReadLine();
        }
    }

    private void Run()
    {
        // Create a new WSHttpBinding and set the security mode to Message;
        WSHttpBinding b = new WSHttpBinding(SecurityMode.Message);

        // Create a ServiceHost instance, and add a metadata endpoint.
        Uri baseUri= new Uri("http://localhost:1008/");
        ServiceHost sh = new ServiceHost(typeof(Calculator), baseUri);

        // Optional. Add a metadata endpoint. The method is defined below.
        AddMetadataEndpoint(ref sh);

        // Add an endpoint using the binding, and open the service.
        sh.AddServiceEndpoint(typeof(ICalculator), b, "myCalculator");
        sh.Open();

        Console.WriteLine("Listening...");
        Console.ReadLine();
    }

    private void AddMetadataEndpoint(ref ServiceHost sh)
    {
        Uri mex = new Uri(@"http://localhost:1001/metadata/");
        ServiceMetadataBehavior sm = new ServiceMetadataBehavior();
        sm.HttpGetEnabled = true;
        sm.HttpGetUrl = mex;
        sh.Description.Behaviors.Add(sm);
    }
}

// This class will cause an InvalidDataContractException to be thrown because
// neither the DataContractAttribute nor DataMemberAttribute has been applied to it.
public class ExtraData
{
    public System.Collections.Generic.List<string> RandomData;
}

[ServiceContract(ProtectionLevel=System.Net.Security.ProtectionLevel.EncryptAndSign) ]
interface ICalculator
{
    [OperationContract]
    double Add(double a, double b);

    [OperationContract]
    ExtraData MoreData();
}

public class Calculator : ICalculator
{
    public double Add(double a, double b)
    {
        return a + b;
    }

    public ExtraData MoreData()
    {
        ExtraData ex = new ExtraData();
        ex.RandomData.Add("Hello");
        ex.RandomData.Add( "World" );
        return ex;
    }
}
Public Class Test
    
    Shared Sub Main() 
        Try
            Dim t As New Test()
            t.Run()
        
        ' Catch the InvalidDataContractException here.
        Catch iExc As InvalidDataContractException
            Console.WriteLine("You have an invalid data contract: ")
            Console.WriteLine(iExc.Message)
            Console.ReadLine()
        
        Catch exc As Exception
            Console.WriteLine(exc.Message)
            Console.WriteLine(exc.ToString())
            Console.ReadLine()
        End Try
    
    End Sub 
    
    Private Sub Run() 
        ' Create a new WSHttpBinding and set the security mode to Message;
        Dim b As New WSHttpBinding(SecurityMode.Message)
        
        ' Create a ServiceHost instance, and add a metadata endpoint.
        Dim baseUri As New Uri("http://localhost:1008/")
        Dim sh As New ServiceHost(GetType(Calculator), baseUri)
        
        ' Optional. Add a metadata endpoint. The method is defined below.
        AddMetadataEndpoint(sh)
        
        ' Add an endpoint using the binding, and open the service.
        sh.AddServiceEndpoint(GetType(ICalculator), b, "myCalculator")
        sh.Open()
        
        Console.WriteLine("Listening...")
        Console.ReadLine()
    
    End Sub 
    
    Private Sub AddMetadataEndpoint(ByRef sh As ServiceHost) 
        Dim mex As New Uri("http://localhost:1001/metadata/")
        Dim sm As New ServiceMetadataBehavior()
        sm.HttpGetEnabled = True
        sm.HttpGetUrl = mex
        sh.Description.Behaviors.Add(sm)
    
    End Sub 
End Class 

' This class will cause an InvalidDataContractException to be thrown because
' neither the DataContractAttribute nor DataMemberAttribute has been applied to it.

Public Class ExtraData
    Public RandomData As System.Collections.Generic.List(Of String)
End Class 

<ServiceContract(ProtectionLevel := System.Net.Security.ProtectionLevel.EncryptAndSign)>  _
Interface ICalculator
    <OperationContract()>  _
    Function Add(ByVal a As Double, ByVal b As Double) As Double 
    
    <OperationContract()>  _
    Function MoreData() As ExtraData 
End Interface 

Public Class Calculator
    Implements ICalculator
    
    Public Function Add(ByVal a As Double, ByVal b As Double) As Double _
    Implements ICalculator.Add
        Return a + b
    End Function 
        
    Public Function MoreData() As ExtraData Implements ICalculator.MoreData
        Dim ex As New ExtraData()
        ex.RandomData.Add("Hello")
        ex.RandomData.Add("World")
        Return ex
    End Function 
End Class

備註

如果 DataContractSerializerNetDataContractSerializer 偵測到要序列化或還原序列化的執行個體型別無效時,就會擲回這個型別的執行個體。 如果結構描述匯入或匯出引擎中傳入了無效的型別,這時也會擲回這個型別的執行個體。

建構函式

InvalidDataContractException()

初始化 InvalidDataContractException 類別的新執行個體。

InvalidDataContractException(SerializationInfo, StreamingContext)

使用指定的 InvalidDataContractExceptionSerializationInfo,初始化 StreamingContext 類別的新執行個體。

InvalidDataContractException(String)

使用指定的錯誤訊息,初始化 InvalidDataContractException 類別的新執行個體。

InvalidDataContractException(String, Exception)

使用指定的錯誤訊息和內部例外狀況,初始化 InvalidDataContractException 類別的新執行個體。

屬性

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。

(繼承來源 Exception)
Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已過時。

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱