Mono 中反序列化的问题

发布于 2024-08-22 07:06:17 字数 1053 浏览 6 评论 0原文

任何人都知道为什么 C# 中的数据协定序列化器生成的以下 XML 在 Windows 中运行良好,但在 Mono 上的 Linux 下运行不佳?

XML:

<Message i:type="UserMessage" xmlns="http://schemas.datacontract.org/2004/07/NetTunnel"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><type>UserMessage</type>
<nick>Unnamed</nick><services><Service><enabled>true</enabled><port_ranges i:nil="true"/>
<service_name>vent</service_name></Service></services><state>Created</state>
<userid>1</userid></Message>

错误:

Unhandled Exception: System.Runtime.Serialization.SerializationException: Deserializing 
type 'System.Object'. Expecting state 'EndElement'. Encountered state 'Element' with 
name 'enabled' with namespace 'http://schemas.datacontract.org/2004/07/NetTunnel'.

如果没有列出服务(xml 标记),它也会给我一个错误。 services 变量的类型为List。这只是 Mono 无法处理的类型吗?另一种类型会更合适吗?或者完全是另外一回事?

Anyone have any idea why the following XML generated by a data contract serializer in C# works just fine in Windows but not under Linux on Mono?

The XML:

<Message i:type="UserMessage" xmlns="http://schemas.datacontract.org/2004/07/NetTunnel"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><type>UserMessage</type>
<nick>Unnamed</nick><services><Service><enabled>true</enabled><port_ranges i:nil="true"/>
<service_name>vent</service_name></Service></services><state>Created</state>
<userid>1</userid></Message>

The error:

Unhandled Exception: System.Runtime.Serialization.SerializationException: Deserializing 
type 'System.Object'. Expecting state 'EndElement'. Encountered state 'Element' with 
name 'enabled' with namespace 'http://schemas.datacontract.org/2004/07/NetTunnel'.

It also gives me an error if there are no services listed (xml tag <services/>). The services variable is of type List<Service>. Is this just a type Mono can't handle? Would another type be more appropriate? Or is it something else entirely?

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

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

发布评论

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

评论(3

脱离于你 2024-08-29 07:06:17

这不是您想要的答案,但是:据我所知,Mono 上与 .Net 不同的每个行为都是 Mono 中的一个 bug。请(!)将其归档,特别是如果它确实很小且易于复制的话。在这里讨论它可能会同样快地有所帮助,但是下一个人可能会遇到同样的问题,需要研究这个问题等等。

只是 将其归档,Mono 人员非常棒,并且会提供更多帮助。为了帮助其他人进行搜索:我建议您使用该问题的链接更新您的帖子。

关于你的最后一个问题:你不需要仅仅因为 Mono 可能需要反序列化它就需要更改类型 - 并且 List 就可以了。

Not the answer you want, but: Every behavior on Mono that is different from .Net is for all I know a bug in Mono. Please(!) file it, especially if it is really that small and easy to reproduce. Discussing it here might help just as fast, but the next guy might run into the same problem, needs to research the issue etc..

Just file it, the Mono guys are awesome and will be more helpful. To aid others in their search: I suggest you update your post with the link to the issue.

Regarding your last question: You shouldn't need to change the type just because Mono might need to deserialize it - and List is just fine.

零度℉ 2024-08-29 07:06:17

从评论来看,您不需要WCF,只是想共享数据。在这种情况下,我会查看 protobuf-net。我很有偏见(因为我是作者),但它是免费的(有来源),所以我不会出于自我增益而推荐它。简单地;它是一种快速、可移植的序列化 API,仿照 google 的“协议缓冲区”数据格式建模。处理速度非常快(例如,通常比 xml 快得多)并且在线上非常小。

如果您已经使用数据契约,您可能可以通过添加唯一的 Order 值(它使用它作为数字标识符)来调整它们:(

[DataContract]
public class Foo {
    [DataMember(Order = 1)]
    public int Id {get;set;}

    [DataMember(Order = 2)]
    public string Name {get;set;}
}

或者您可以使用特定的 protobuf-net 属性)

完整的源代码可用,它适用于 Mono、常规 .NET、CF 等。

From the comments, you don't need WCF and just want to share data. In which case, I would look at protobuf-net. I'm quite biased (since I'm the author), but it is free (with source), so I'm not recommending it from self gain. Simply; it is a fast, portable serialization API modelled after google's "protocol buffers" data format. Very fast to process (typically much faster than xml, for example) and pretty small on the wire.

If you already using data-contracts, you might be able to tweak them simply by adding unique Order values (it uses this as a numeric identifier):

[DataContract]
public class Foo {
    [DataMember(Order = 1)]
    public int Id {get;set;}

    [DataMember(Order = 2)]
    public string Name {get;set;}
}

(or you can use the specific protobuf-net attributes)

The full source is available, and it works on Mono, regular .NET, CF, etc.

原谅我要高飞 2024-08-29 07:06:17

已在此处提交错误报告:https://bugzilla.novell.com/show_bug.cgi? id=581611

不过,在这个问题得到解决之前,如果能知道任何替代方案,那就太好了。

Submitted a bug report here: https://bugzilla.novell.com/show_bug.cgi?id=581611

Would be great to know of any alternatives until this is fixed, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文