Silverlight、EDM/LINQ 和 WCF Web 服务 - 如何将集合传递到 Silverlight
将答案(集合)传递回 Silverlight 的正确方法是什么?
例如,如果我有一个位于 Northwind 示例数据库之上的服务应用程序,并且该服务有一个名为 GetEmployees() 的方法。 传回 Silverlight 的正确“事物”是什么? 一个 IQueryable ?
然后考虑 Silverlight 端的异步/结果转换内容,我也该转换什么? 一个 IQueryable ?
更新:
是不是声明 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 允许远程客户端查看服务中的结构(实体)? 我不知道有 Silverlight 友好的 WCF 类之类的东西,所以这不是我开始的地方。 一旦我在解决方案中添加该属性,如果没有它,我将无法再使用该服务。 所以我无法测试我之前看到的内容。 关于 AspNetCompatibilityRequirementsMode.Allowed 的底层功能有什么想法吗?
更新2:
请参阅特里·多纳吉 (Terry Donaghe) 的评论。
What is the proper way to pass an answer (a collection) back to Silverlight?
For example, if I have a service application that sits on top of the Northwind sample database and the service has a method called GetEmployees(). What is the proper "thing" to pass back to Silverlight? An IQueryable ?
Then considering the Async/Result casting stuff on the Silverlight side what do I cast it too? An IQueryable ?
UPDATE:
Is it the declaration of
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] that allows a remote client to see the structs (entities) from a service?? I didn't know there was such thing as a Silverlight-friendly-WCF class so thats not what I started with. Once I added that attribute in the solution wouldn't let me use the service any more without it. So I could not test what I was seeing before. Any thoughts on what AspNetCompatibilityRequirementsMode.Allowed does under the hood?
UPDATE 2:
See comments to Terry Donaghe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否刚刚尝试传递基本类型的数组? 除非您创建 DataContracts,否则 WCF 不知道您的任何对象是什么。 当您尝试传递 List WCF 时,实际上只是传递一个数组,因为它被设计为尝试可互操作。
定义一个 DataContract 以包含有关员工(或其他)的信息。 尽量保持相对简单。 当您创建代理时(可能使用 svcutil 或添加服务引用),VS2008 将自动在您的客户端神奇地定义 DataContracts,然后您可以像常规对象一样使用它们。
我更喜欢手动使用 WCF - 我创建自己的合约、实现和代理 dll。 就反序列化和其他方面而言,这样做给了我很大的灵活性。 有关详细信息,请参阅以下两个参考资料:
WCF 手动方式,正确方式
手动 WCF - 扩展
另外,请参阅 WCF 圣经第 3 章“WCF 编程”服务”作者:Juval Lowy。 它充满了有关 DataContracts 的信息。
Did you just try passing arrays of base types around? Unless you create DataContracts, WCF doesn't know what any of your objects are. When you try to pass a List WCF really just passes an array since it's designed to try to be inter-operable.
Define a DataContract to contain information about the Employees (or whatever). Try to keep it relatively simple. When you create your proxy (probably with svcutil or Add Service Reference) VS2008 will auto-magically define the DataContracts on your client side and then you can use them just like a regular object.
I prefer to use WCF manually - I create my own contract, implementation and proxy dlls. Doing that gives me a great deal of flexibility as far as de-serialization and other stuff is concerned. For more on that, see these two references:
WCF the Manual Way, the Right Way
Manual WCF - an Extension
Also, please see Chapter 3 of the WCF Bible, "Programming WCF Services" by Juval Lowy. It's chock full of info on DataContracts.
在使用常规 Web 服务时,您必须返回诸如列出 SOAP 格式化程序之类的集合才能知道如何处理它。 但是,我没有使用过 WCF,所以我不知道这是否会有所不同。
In using regular web services, you have to return collections like List the SOAP formatter to know what to do with it. However, I have not used WCF, so I don't know if that does things differently.
只需返回一个列表即可。 如果您可以返回单个T,那么您可以返回一个List<T>。
Just return a List. If you can return a single T, then you can return a List<T>.