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

发布于 2024-08-09 01:54:11 字数 1472 浏览 6 评论 0原文

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

我的问题是,从 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.

I have application created using WCF and C#, it's architecture requires to add KnownTypes through App.config. 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:

  <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).
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-08-16 01:54:11

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

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

Possible mismatch of the types between the server and the client?

Maybe if you post the exception details we can be of more help.

巷子口的你 2024-08-16 01:54:11

我会尝试什么:

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

伪代码:

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

What I would try:

  1. Check if MethodDeclaration has DataContractAttribute and properties DataMemberAttribute
  2. Add [ServiceKnownType(typeof(MethodDeclaration))] to service class

Pseudo code:

[ServiceContract]
public interface IService
{
    [ServiceKnownType(typeof(MethodDeclaration))]
    void ProcessMethodDeclaration(Declaration val);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文