我们可以将Odata元数据转换为C#代码吗(DataContract) [英] can we convert Odata metadata to C# code (datacontract)

查看:147
本文介绍了我们可以将Odata元数据转换为C#代码吗(DataContract)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个odata网络服务,我可以从

there is a odata webservice, i can get metadata from

http://service-uri.com/$metadata

包含其所有模型对象的定义,

which contains definition of all its model object,

我可以使用任何工具将元数据xml转换为C#中的数据协定类吗?

is there any tools that i can use, to convert that metadata xml to data contract class in C#?

推荐答案

WCF数据服务客户端库可以从$ metadata终结点为您生成C#类.这些类没有[DataContract]属性,但是您可以将它们与客户端库的其余部分一起使用,以与服务进行交互.

The WCF Data Services Client library can generate C# classes for you from the $metadata endpoint. These classes won't have the [DataContract] attribute, but you can use them with the rest of the client library to interact with the service.

要使用此功能,请下载客户端库的最新工具"安装程序,当前可在此处找到:

To use this feature, download the latest "tools" installer for the client library, currently available here: http://www.microsoft.com/en-us/download/details.aspx?id=35840

(注意:如果需要客户端库的最新稳定位,还请从Visual Studio的程序包管理器控制台"窗口中运行以下命令:Install-Package Microsoft.Data.Services.Client)

(Note: If you want the latest stable bits of the client library, also run the following from the Package Manager Console window in Visual Studio: Install-Package Microsoft.Data.Services.Client)

然后,在Visual Studio中,您可以右键单击您的项目,然后选择添加服务引用".在这里,您可以输入服务的$ metadata URL,然后将生成相应的类以及可用于与服务交互的DataServiceContext类.有关查询服务的更多信息,请参见以下文档: http://msdn.microsoft .com/en-us/library/dd673933.aspx

Then, from within Visual Studio, you can right click on your project, and select "Add Service Reference". Here you can enter the $metadata URL of the service, and the corresponding classes will be generated, along with a DataServiceContext class you can use to interact with the service. For more info on querying the service, see this documentation: http://msdn.microsoft.com/en-us/library/dd673933.aspx

或者,如果要手动生成类,则可以使用工具安装程序随附的DataSvcUtil.exe命令行工具.例如,在我的系统上,该文件位于 C:\Program Files (x86)\Microsoft WCF Data Services\Current\bin\tools\DataSvcUtil.exe

Or, if you want to generate the classes manually, you can use the DataSvcUtil.exe command line tool that comes with the tools installer. On my system, for example, that file is located at C:\Program Files (x86)\Microsoft WCF Data Services\Current\bin\tools\DataSvcUtil.exe

有关如何使用此工具的说明,请访问以下页面: http://msdn.microsoft.com/zh-cn/library/dd756369.aspx

For instructions on how to use this tool, take a look at this page: http://msdn.microsoft.com/en-us/library/dd756369.aspx

这篇关于我们可以将Odata元数据转换为C#代码吗(DataContract)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆