意外的类型 - 序列化异常 [英] Unexpected Type - Serialization Exception

查看:118
本文介绍了意外的类型 - 序列化异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适当的WCF服务。



正常运行会看到服务器通过回调做一些处理返回填充XactTaskIn对象给客户端。我有这样好的工作。



我的问题是,当我尝试和returnData变量设置为填充XactEx​​ception,并尝试通过回调的XactTaskIn发送回客户端,我得到抛出下面的异常




例外 - 型XactEx​​ception'与
数据协定名称
'预计不会//schemas.datacontract.org/2004/07/'
是考虑使用
DataContractResolver或添加任何类型的
不是静态已知$ b名单:XactEx​​ception:HTTP。 $ b已知类型 - 例如,通过使用
上KnownTypeAttribute属性或由
。加入他们已知类型
传递给DataContractSerializer列表。
(System.Runtime.Serialization.SerializationException)异常
消息=型'XactEx​​ception'与
数据协定名称
'XactEx​​ception:HTTP://schemas.datacontract.org/ 2004/07 /'预计不会
是考虑使用
DataContractResolver或添加任何类型的
不是静态已知
已知类型的列表 - 例如,通过使用
将KnownTypeAttribute属性或由
将它们添加到已知类型
传递给DataContractSerializer列表。,
异常类型=
System.Runtime.Serialization.SerializationException




下面是XactTaskIn类

  [DataContract] 
公共类XactTaskIn
{
[数据成员]
公众的DateTime超时;
[数据成员]
公众的DateTime timeIn;
[数据成员]
公共字符串名称;
[数据成员]
公共字符串responseTo;
[数据成员]
公共字符串moduleFromName;
[数据成员]
公共字符串moduleFromType;
[数据成员]
公共字符串methodFromName;
[数据成员]
公共对象[] originalInputs;
[数据成员]
公共字符串returnMethodToCall;
[数据成员]
公共字符串returnModuleToCall;
[数据成员]
公共对象returnData;

公共XactTaskIn(日期时间超时,日期timeIn,字符串名称,字符串responseTo,字符串moduleFromName,字符串moduleFromType,字符串methodFromName,对象[] originalInputs,字符串returnMethodToCall,字符串returnModuleToCall,对象returnData)
{
this.timeOut =超时;
this.timeIn = timeIn;
this.name =名称;
this.responseTo = responseTo;
this.moduleFromName = moduleFromName;
this.moduleFromType = moduleFromType;
this.methodFromName = methodFromName;
this.originalInputs = originalInputs;
this.returnMethodToCall = returnMethodToCall;
this.returnModuleToCall = returnModuleToCall;
this.returnData = returnData;
}
}

下面是XactEx​​ception类:

  [DataContract] 
公共类XactEx​​ception
{
[数据成员]
字符串消息;

公共XactEx​​ception(字符串消息)
{
this.message =消息;
//添加实施。
}
}



更新:



好了,所以从丹尼尔的评论帮助了我。



现在看起来像服务器发送回调到客户端,但客户端抛出以下异常。





  • 捉住:尝试反序列化
    消息格式化抛出一个异常:有,而
    尝试反序列化参数$错误b $ b http://tempuri.org/:taskIn 。在
    的InnerException消息是在
    错误1号线位置960元
    'http://schemas.datacontract.org/2004/07/:returnData'
    包含通过映射到
    名称
    型数据http://schemas.datacontract.org/2004/07/:XactEx​​ception'。
    解串器没有$ b $知识b映射到该名称的任何类型的
    考虑使用DataContractResolver
    或添加对应于
    位XactEx​​ception'已知
    类型的列表的类型 - 例如,通过使用
    KnownTypeAttribute属性或由
    将其添加到列表已知类型的
    传递给DataContractSerializer的。。
    请参阅的InnerException更多
    的细节。
    (System.ServiceModel.Dispatcher.NetDispatcherFaultException)异常
    消息=格式化抛出
    异常尝试反序列化
    的消息:有,而
    尝试反序列化参数
    http://tempuri.org/: taskIn 的。在
    的InnerException消息是在
    线1的位置960元
    错误'http://schemas.datacontract.org/2004/07/:returnData'
    包含数据映射到
    名称
    'http://schemas.datacontract.org/2004/07/:XactEx​​ception一个类型。
    解串器没有$ B $知识b映射到该名称的任何类型。
    考虑使用DataContractResolver
    或添加对应于
    位XactEx​​ception'已知
    类型的列表的类型 - 例如,通过使用
    KnownTypeAttribute属性或由
    将它添加到传​​递给DataContractSerializer的已知类型
    的列表。。
    请参阅的InnerException更多
    的细节。异常类型=
    System.ServiceModel.Dispatcher.NetDispatcherFaultException


< /块引用>
解决方案

在你的类

  [DataContract] 
公共类XactTaskIn

您返回对象的属性:

  [数据成员] 
公共对象[] originalInputs;

[数据成员]
公反对returnData;

WCF需要提前知道什么类型都不可能在那里,所以它可以告诉客户端(通过WSDL)什么都的类型。对于任何/(即心不是整型,字符串,日期时间等任何东西)所有非'native类型,您将需要添加一个[KnownType]属性为每一个可能的可能在这些对象属性传递回来,像这种类型的:

  [KnownType(typeof运算(XactEx​​ception))] 
〔KnownType(typeof运算(...))]
[KnownType(typeof运算(...))]
[DataContract]
公共类XactTaskIn

在WCF建立在服务的WSDL这种方式,它会知道XactEx​​ception添加到数据类型列表中,同时串行会知道。寻找那些类






侧面说明;如果你的客户是使用SrvUtil,服务引用而建,或以某种方式生成WSDL,您将需要添加[KnownType]属性后重建客户!


I have a WCF service in place.

Normal operation would see the server doing some processing the returning a populated XactTaskIn object to the client via a callback. I have this working ok.

My problem is that when I try and set the returnData variable to a populated XactException and try to send the XactTaskIn back to the client via the callback, I get the following exception thrown.

Exception - "Type 'XactException' with data contract name 'XactException:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer." (System.Runtime.Serialization.SerializationException) Exception Message = "Type 'XactException' with data contract name 'XactException:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.", Exception Type = "System.Runtime.Serialization.SerializationException"

Here is the XactTaskIn class

[DataContract]
public class XactTaskIn
{
    [DataMember]
    public DateTime timeOut;
    [DataMember]
    public DateTime timeIn;
    [DataMember]
    public string name;
    [DataMember]
    public string responseTo;
    [DataMember]
    public String moduleFromName;
    [DataMember]
    public String moduleFromType;
    [DataMember]
    public String methodFromName;
    [DataMember]
    public object[] originalInputs;
    [DataMember]
    public String returnMethodToCall;
    [DataMember]
    public String returnModuleToCall;
    [DataMember]
    public object returnData;

    public XactTaskIn(DateTime timeOut, DateTime timeIn, string name, string responseTo, String moduleFromName, String moduleFromType, String methodFromName, object[] originalInputs, String returnMethodToCall, String returnModuleToCall, object returnData)
    {
        this.timeOut = timeOut;
        this.timeIn = timeIn;
        this.name = name;
        this.responseTo = responseTo;
        this.moduleFromName = moduleFromName;
        this.moduleFromType = moduleFromType;
        this.methodFromName = methodFromName;
        this.originalInputs = originalInputs;
        this.returnMethodToCall = returnMethodToCall;
        this.returnModuleToCall = returnModuleToCall;
        this.returnData = returnData;
    }
}

Here is the XactException class:

[DataContract]    
public class XactException
{
    [DataMember]
    string message;

    public XactException(string message)
    {
        this.message = message;
        // Add implementation.
    }
}

Update:

Ok so the comment from Daniel has helped me.

It looks now like the server is sending the callback to the client, but the client is throwing the following exception.

  • Caught: "The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:taskIn. The InnerException message was 'Error in line 1 position 960. Element 'http://schemas.datacontract.org/2004/07/:returnData' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/:XactException'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'XactException' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details." (System.ServiceModel.Dispatcher.NetDispatcherFaultException) Exception Message = "The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:taskIn. The InnerException message was 'Error in line 1 position 960. Element 'http://schemas.datacontract.org/2004/07/:returnData' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/:XactException'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'XactException' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.", Exception Type = "System.ServiceModel.Dispatcher.NetDispatcherFaultException"

解决方案

In your class

    [DataContract]
    public class XactTaskIn

you have properties that return objects:

        [DataMember]
        public object[] originalInputs;

        [DataMember]
        public object returnData;

WCF needs to know ahead of time what types can possibly be in there, so that it can tell the client (through the WSDL) what all the types are. For any/all non-'native' types (anything that isnt int, string, DateTime, etc) you will need to add a [KnownType] attribute for every possible type that might be passed back in those object properties, like this:

    [KnownType(typeof(XactException))]
    [KnownType(typeof(...))]
    [KnownType(typeof(...))]
    [DataContract]
    public class XactTaskIn

That way when WCF builds the WSDL for the service, it will know to add XactException to the list of datatypes, and also the serializer will know to look for those classes.


Side-note; if your client was built using SrvUtil, Service Reference, or generated from the WSDL in some way, you will need to rebuild the client after adding the [KnownType] attributes!

这篇关于意外的类型 - 序列化异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆