InvalidDataContractException 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
DataContractSerializer 或 NetDataContractSerializer 在序列化和反序列化的过程中遇到无效的数据协定时引发的异常。
public ref class InvalidDataContractException : Exceptionpublic class InvalidDataContractException : Exception[System.Serializable]
public class InvalidDataContractException : Exceptiontype InvalidDataContractException = class
    inherit Exception[<System.Serializable>]
type InvalidDataContractException = class
    inherit ExceptionPublic Class InvalidDataContractException
Inherits Exception- 继承
- 属性
示例
下面的示例演示了服务协定 ICalculator,它包含 Add 和 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
注解
如果 DataContractSerializer 或 NetDataContractSerializer 检测到正在序列化或反序列化的实例的类型无效,则会引发该类型的实例。 如果向架构导入或导出引擎传递了无效类型,也会引发该类型的实例。
构造函数
| InvalidDataContractException() | 初始化 InvalidDataContractException 类的新实例。 | 
| InvalidDataContractException(SerializationInfo, StreamingContext) | 
				已过时.
			 使用指定的 InvalidDataContractException 和 SerializationInfo 初始化 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) |