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

时间:2009-10-22 17:52:55

标签: c# .net wcf datacontractserializer

我使用WCF和C#创建了应用程序,它的架构要求通过App.config添加KnownTypes。我有这样的服务: 客户 - > 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>

我的问题是,调用方法whitch从CentralServer获取DataServer类型上的MethodDeclaration类型的参数会引发一个异常,即服务无法反序列化此参数,尽管CentralServer CAN序列化它(我知道,因为当我删除KnownType时,我得到异常服务不能连载)。 另外,我有很多这样的方法定义相同的方式,但接受不同的类型作为参数,并且它们完美地工作。 你知道为什么这不是这样的工作吗?

例外:

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.

2 个答案:

答案 0 :(得分:1)

服务器和客户端之间的类型可能不匹配?

也许如果您发布异常详情,我们可以提供更多帮助。

答案 1 :(得分:0)

我会尝试:

  1. 检查MethodDeclaration是否具有DataContractAttribute和属性DataMemberAttribute
  2. 将[ServiceKnownType(typeof(MethodDeclaration))]添加到服务类
  3. 伪代码:

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