DataContractSerializer C# 可以序列化,不能反序列化,为什么?
我有使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
服务器和客户端之间的类型可能不匹配?
也许如果您发布异常详细信息,我们可以提供更多帮助。
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:
Pseudo code: