0

我有使用 WCF 和 C# 创建的应用程序,它的架构需要通过 App.config 添加 KnownTypes。我有这样的服务: Client -> 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 可以对其进行序列化(我知道,因为当我删除 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.
4

2 回答 2

1

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

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

于 2009-10-23T14:30:35.430 回答
0

我会尝试什么:

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

伪代码:

[ServiceContract]
public interface IService
{
    [ServiceKnownType(typeof(MethodDeclaration))]
    void ProcessMethodDeclaration(Declaration val);
}
于 2017-06-23T09:37:56.833 回答