ObservableCollection 使用 WCF 传输后变成数组
我有一个名为“Board”的类,它的属性之一是 ObservableCollection。当我通过 WCF(从服务器到客户端)发送 ObservableCollection 并从我的代理调用它时,它会变成一个数组,这对我来说没有好处。
我可以在发送后保留 ObservableCollection 吗?还是必须将数组踢出,直到它再次成为 ObservableCollection?
I got a class called "Board" and one of its property's is an ObservableCollection. When i send the ObservableCollection through WCF (from server to client) end call it from my proxy, it's turned into an Array, which is no good for me.
Can i keep the ObservableCollection after being sent, or do i have to kick the Array till it becomes an ObservableCollection again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 VS 上下文菜单中的“配置服务引用”选项以供参考。您可以选择通过服务传输的集合类型。默认情况下,我认为它设置为数组,但有多种选择(我相信列表和可观察集合是选项)。
编辑:我刚刚检查过,不幸的是可观察的集合不是选择之一。看起来你必须选择:
Check out the 'Configure Service Reference' option in the context menu in VS for the reference. You can choose the collection type that is transmitted across the service. By default I think it is set to array but there are several choices (I believe list and observablecollection are options).
EDIT: I just checked, and unfortunately observable collection is not one of the choices. It looks like you'll have to pick from:
默认情况下 - 不,你对此无能为力。 WCF 会将您的结构序列化为可以用 XML 模式表示的内容。 XML Schema 除了原始且相当简单的数据结构之外什么都不知道。您只能传输具体的原始数据 - 没有“神奇”的行为插件。
该问题有一个解决方案,如果您拥有线路的两端:您可以将服务和数据协定放入单独的类库程序集中,并在服务器和客户端之间共享它们。在这种情况下,您的数据契约只有一个实现 - 您的 ObservableCollection。
如果您在服务(实现)类和客户端之间共享该程序集(在从 Visual Studio 中“添加服务引用”之前添加对该程序集的引用!),那么您的客户端应该选择该程序集ObservableCollection 并继续使用它(而不是在客户端创建 XML 模式兼容的数组)。
By default - no, you cannot do anything about it. WCF will serialize your structures into something that can be represented with XML schema. XML Schema has no knowledge of anything but raw, and fairly simplistic data structures. You can only transfer concrete, raw data - no "magic" behavioral addon.
There is one solution to the problem, IF you own both ends of the wire: you could put your service and data contracts into a separate class library assembly, and share those between server and client. In that case, you only ever have one single implementation of your data contract - your ObservableCollection.
If you share that assembly between your service (implementation) class, and the client (add the reference to that assembly before you "Add Service Reference" from Visual Studio!), then your client should pick up that ObservableCollection and continue to use that (instead of creating a XML schema compatible Array on the client side).
谢谢两位的回答。
当我继续该项目时,我将查看这两种解决方案,并将首先尝试更改通过 wcf 服务发送的集合。
我会让你知道什么对我有用......
Thank you both for the answer.
I will look at both solutions when i continue the project, and will start with try and change the Collection send through the wcf service.
I'll let you know what works for me...