为 REST 对象生成合约
我是 REST 新手,这听起来应该很简单。在 .NET 应用程序中,我可以创建对 WCF 服务的引用,并且将为我生成所有可用类型的协定。
现在我尝试在 Windows Phone 7 应用程序中使用 REST 服务。虽然我可以进行调用并获得正确的响应,但是否有一种简单的方法来创建每个对象将反序列化到的类?
我正在使用 RestSharp 来管理我的通话。在我见过的一些示例中,用户创建了自己的类,并手动生成了 xml。如果可能的话,我想避免这种情况。
非常感谢!
I'm new to REST and this sounds like it should be pretty simple. In a .NET app, I can create a reference to a WCF service and the contracts for all the available types will be generated for me.
Now I'm trying to consume a REST service in a Windows Phone 7 app. While I can make my call and get back the proper response, is there a simple way to create the classes that each object would be deserialized to?
I'm using RestSharp to manage my calls. In some examples I've seen, user's have created their own classes, and generated the xml manually. I would like to avoid this if at all possible.
many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的响应是 XML,您可以将 xml 保存到文件中,然后对其调用 xsd.exe 以生成架构。在架构上调用 xsd.exe,它将生成 ac# 类文件,您可以从 xml 序列化和反序列化。以下是有关 XSD.exe 工作原理的文档:
http: //msdn.microsoft.com/en-us/library/x6c1kb0s(v=VS.100).aspx
Assuming your response is XML, you can save the xml into a file, then call xsd.exe on it to generate a schema. Call xsd.exe on the schema and it will generate a c# class file you can seriazlize and deserialize to from the xml. Here's the documeantion on how XSD.exe works:
http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=VS.100).aspx
您必须生成响应数据将映射到的类(或者如果您使用的是 .NET 4,则使用动态反序列化方案),因为 REST 不包括 SOAP 那样的架构定义系统。在 RestSharp 中,有一个 T4 帮助器可以更轻松地生成 C# 类。它可以帮助您完成大约 80% 的工作。如果您需要任何帮助,请发布到 RestSharp Google 网上论坛。
You have to generate the classes that your response data will map to (or use a dynamic deserialization scheme if you're on .NET 4) since REST does not include a schema definition system the way SOAP does. In RestSharp, there's a T4 helper to make generating the C# classes easier. It gets you about 80% of the way there. If you need any help with it, post to the RestSharp Google Group.