InvalidDataContractException 클래스

정의

serialization 및 deserialization 중에 DataContractSerializer 또는 NetDataContractSerializer가 잘못된 데이터 계약을 발견한 경우 throw되는 예외입니다.

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
특성

예제

다음 예제에서는 두 가지 메서드 Add 를 포함하는 서비스 계약을 ICalculator보여 줍니다MoreData. 메서드는 MoreData 코드에 선언된 클래스의 ExtraData 인스턴스를 반환합니다. 구현된 인터페이스 Calculator 가 실행 InvalidDataContractException 되면 클래스에 ExtraData 적용된 인터페이스가 없기 DataContractAttribute 때문에 throw됩니다.

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

설명

직렬화되거나 NetDataContractSerializer 역직렬화되는 인스턴스의 형식이 유효하지 않은 경우 DataContractSerializer 이 형식의 인스턴스가 throw됩니다. 잘못된 형식이 스키마 가져오기 또는 내보내기 엔진에 전달되면 이 형식의 인스턴스도 throw됩니다.

생성자

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

현재 예외를 throw하는 메서드를 가져옵니다.

(다음에서 상속됨 Exception)

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetBaseException()

파생 클래스에서 재정의된 경우 하나 이상의 후속 예외의 근본 원인이 되는 Exception 을 반환합니다.

(다음에서 상속됨 Exception)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetObjectData(SerializationInfo, StreamingContext)

파생 클래스에서 재정의된 경우 예외에 관한 정보를 SerializationInfo 에 설정합니다.

(다음에서 상속됨 Exception)
GetType()

현재 인스턴스의 런타임 형식을 가져옵니다.

(다음에서 상속됨 Exception)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 예외에 대한 문자열 표현을 만들고 반환합니다.

(다음에서 상속됨 Exception)

이벤트

SerializeObjectState
사용되지 않습니다.

예외에 대한 serialize된 데이터가 들어 있는 예외 상태 개체가 만들어지도록 예외가 serialize될 때 발생합니다.

(다음에서 상속됨 Exception)

적용 대상

추가 정보