简体   繁体   English

DataContractSerializer C#可以序列化,不能反序列化,为什么?

[英]DataContractSerializer C# Can serialize, cannot deserialize, why?

I have application created using WCF and C#, it's architecture requires to add KnownTypes through App.config. 我有使用WCF和C#创建的应用程序,它的体系结构要求通过App.config添加KnownTypes。 I have services going like this: Client -> CentralServer -> DataServer (where -> is WCF connection) Now, I've added KnownTypes to both CentralServer App.config and DataServer App.config this way: 我有这样的服务:客户端-> CentralServer-> DataServer(其中->是WCF连接)现在,我以这种方式将KnownTypes添加到CentralServer App.config和DataServer App.config中:

  <add
 type="Odra.Server.CentralServer.SBQL.AbstractSyntax.Declarations.Declaration,
 Odra.Server.CentralServer.SBQL"> 
 <knownType
 type="Odra.Server.CentralServer.SBQL.AbstractSyntax.Declarations.MethodDeclaration,
 Odra.Server.CentralServer.SBQL" /> 
 </add>

My problem is that, calling method whitch takes argument of type MethodDeclaration on DataServer from CentralServer throws an exception that service cannot deserialize this parameter, although CentralServer CAN serialize it (I know, because when I remove KnownType i get exception that service cannot SERIALIZE). 我的问题是,尽管CentralServer可以序列化该方法,但从CentralServer调用方法whitch接受DataServer上的DataServer类型MethodDeclaration类型的参数时,服务无法反序列化此异常(我知道,因为当我删除KnownType时,我会获得该服务无法SERIALIZE的异常)。 In addition, I have plenty of such methods defined same way, but accepting different type as an argument, and they work perfectly. 另外,我有很多这样的方法以相同的方式定义,但是接受不同的类型作为参数,并且它们完美地工作。 Do you have any idea why this is (not) working like this? 您知道为什么这样(不)工作吗?

Exception: 例外:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:val. 
The InnerException message was 'Element 'http://tempuri.org/:val' contains data of the 'http://schemas.datacontract.org/2004/07/Odra.Server.CentralServer.SBQL.AbstractSyntax.Declarations:MethodDeclaration' data contract. The deserializer has no knowledge of any type that maps to this contract. 
Add the type corresponding to 'MethodDeclaration' 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.

Possible mismatch of the types between the server and the client? 服务器和客户端之间的类型可能不匹配?

Maybe if you post the exception details we can be of more help. 也许如果您发布例外详细信息,我们将为您提供更多帮助。

What I would try: 我会尝试的:

  1. Check if MethodDeclaration has DataContractAttribute and properties DataMemberAttribute 检查MethodDeclaration是否具有DataContractAttribute和属性DataMemberAttribute
  2. Add [ServiceKnownType(typeof(MethodDeclaration))] to service class 将[ServiceKnownType(typeof(MethodDeclaration))]添加到服务类

Pseudo code: 伪代码:

[ServiceContract]
public interface IService
{
    [ServiceKnownType(typeof(MethodDeclaration))]
    void ProcessMethodDeclaration(Declaration val);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM