{"id":413,"date":"2014-03-11T10:48:00","date_gmt":"2014-03-11T10:48:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/odatateam\/2014\/03\/11\/tutorial-sample-how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class\/"},"modified":"2024-02-12T15:53:45","modified_gmt":"2024-02-12T22:53:45","slug":"tutorial-sample-how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/odata\/tutorial-sample-how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class\/","title":{"rendered":"[Tutorial &amp; Sample] How to use OData Client Code Generator to generate client-side proxy class"},"content":{"rendered":"<p><em><span style=\"font-size: x-small;\"><em><span style=\"font-size: x-small;\">Edited on\u00a0July 09, 2014 by updating \u201cAdd OData Client Proxy File\u201d for V2.0.0 and adding &#8220;Appendix&#8221; part. Please refer to &#8220;<a title=\"OData Client Code Generator 2.0.0 release\" href=\"http:\/\/blogs.msdn.com\/b\/odatateam\/archive\/2014\/07\/09\/odata-client-code-generator-2-0-0-release.aspx\">OData Client Code Generator 2.0.0 release<\/a>&#8221; for V2.0.0 release notes<\/span><\/em><\/span><\/em><\/p>\n<p><em><span style=\"font-size: x-small;\">Edited on Apr 29, 2014 by adding &#8220;Upgrade the\u00a0Project Dependencies to the Most Recent Version&#8221; part.<\/span><\/em><\/p>\n<p>In this tutorial, you will generate an OData client proxy class for an OData V4 service by \u201cOData Client Code Generator\u201d.<\/p>\n<h3>Install OData Client Code Generator<\/h3>\n<p>Start Visual Studio, from the <strong>TOOLS<\/strong> menu, select <strong>Extensions and Updates<\/strong><\/p>\n<p>In the left panel, expand <strong>Online -&gt; Visual Studio Gallery.<\/strong> Search <strong>\u201cOData Client Code Generator\u201d <\/strong>in search box and <strong>Download <\/strong>the VSIX.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/6574.download_thumb_50656F12.png\"><img decoding=\"async\" title=\"download\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/6574.download_thumb_50656F12.png\" alt=\"download\" width=\"620\" height=\"430\" border=\"0\" \/><\/a><\/p>\n<p>After the VSIX is downloaded, the installer page will show. Click <strong>Install.<\/strong><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/0882.Install_thumb_2138A4DD.png\"><img decoding=\"async\" title=\"Install\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/0882.Install_thumb_2138A4DD.png\" alt=\"Install\" width=\"618\" height=\"431\" border=\"0\" \/><\/a><\/p>\n<p>Following dialog will show when installation finish. Click <strong>Close. <\/strong><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/6237.Install-Finished_thumb_132167F3.png\"><img decoding=\"async\" title=\"Install Finished\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/6237.Install-Finished_thumb_132167F3.png\" alt=\"Install Finished\" width=\"475\" height=\"356\" border=\"0\" \/><\/a><\/p>\n<p>You need to restart Visual studio in order for the changes to take effect.<\/p>\n<h3>Create Your Application<\/h3>\n<p>Create your project. OData Client Code Generator works with any projects, but here, we take \u201cConsole Application\u201d project for example<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/6404.ConsoleApp_thumb_7D5CA0F7.png\"><img decoding=\"async\" title=\"ConsoleApp\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/6404.ConsoleApp_thumb_7D5CA0F7.png\" alt=\"ConsoleApp\" width=\"600\" height=\"419\" border=\"0\" \/><\/a><\/p>\n<h3>Add OData Client Proxy File<\/h3>\n<p>In <strong>Solution Explorer, <\/strong>right click you project name. Click <strong>Add-&gt;New Item.<\/strong><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5661.AddItem_thumb_62B52640.png\"><img decoding=\"async\" title=\"AddItem\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5661.AddItem_thumb_62B52640.png\" alt=\"AddItem\" width=\"609\" height=\"373\" border=\"0\" \/><\/a><\/p>\n<p>In the left panel, expand <strong>Installed-&gt;Visual C# Items-&gt;Code<\/strong>. Choose \u201c<strong>OData Client<\/strong>\u201d, and rename your client file, such as \u201cNorthwindProxy.tt\u201d, and Click <strong>Add.<\/strong><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/2376.RenameItem_thumb_4879DE7E.png\"><img decoding=\"async\" title=\"RenameItem\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/2376.RenameItem_thumb_4879DE7E.png\" alt=\"RenameItem\" width=\"612\" height=\"367\" border=\"0\" \/><\/a><\/p>\n<p>This step will add two files to your project<\/p>\n<p>1. <strong><em>NorthwindProxy.tt<\/em><\/strong> contains all the configurations you need to set:<\/p>\n<ul>\n<li><strong>MetadataDocumentUri<\/strong><\/li>\n<\/ul>\n<blockquote><p>Service document URI or service metadata URI or a file path of metadata local copy.<\/p><\/blockquote>\n<ul>\n<li><strong>NamespacePrefix<\/strong><\/li>\n<\/ul>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>The value will be used as the namespace of your client proxy when metadata contains only one schema.<\/li>\n<li>The value will be added before namespaces of schemas when metadata contains several schemas.<\/li>\n<li>The namespace in metadata will be used if the value is empty string.<\/li>\n<\/ul>\n<\/li>\n<li><strong>EnableNamingAlias<\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px;\">\u00a0\u00a0 This value will be used to enable naming alias. By default, it is to enable camel case on client side. But user can customize the\u00a0naming alias logic.<\/p>\n<p>2.<strong><em> NorthwindProxy.ttinclude<\/em><\/strong> is the T4 template for generating proxy file from metadata document.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/6165.V2.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/6165.V2.jpg\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>Save the tt file, The generated cs file will show in Solution Explorer under the tt file.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/0020.ge2_.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/0020.ge2_.jpg\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<h3>Consume the generated code<\/h3>\n<p>Now, we have generated the client proxy for the service file. You can refer to it in your client code. Example.\n<script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/Laylaliu\/ec8d5a114e6e46bf726a.js\"><\/script><\/p>\n<div>\n<h3>Upgrade the Project Dependencies to the Most Recent Version<\/h3>\n<\/div>\n<p>After all the steps above, you will find that the OData core and client libraries version 6.0.0 have been added into the references of this project.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/8816.Reference.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/8816.Reference.png\" alt=\"\" width=\"496\" height=\"584\" border=\"0\" \/><\/a><\/p>\n<p>If you want to upgrade the OData core and client libraries to the most recent version, you can use the NuGet Package Manager to do it.<\/p>\n<p>In the solution explorer, right click the project and click <strong>&#8220;Manage NuGet Packages&#8221;.<\/strong><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5100.ManageNuget.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5100.ManageNuget.png\" alt=\"\" width=\"496\" height=\"641\" border=\"0\" \/><\/a><\/p>\n<p>In the left panel, expand <strong>Update <\/strong>-&gt; <strong>nuget.org<\/strong>.<strong>\u00a0<\/strong>Select &#8220;<strong>OData Client for .NET&#8221; <\/strong>and click <strong>&#8220;Update&#8221;<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/0844.UpdateNugetPackage.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/0844.UpdateNugetPackage.png\" alt=\"\" width=\"497\" height=\"334\" border=\"0\" \/><\/a><\/p>\n<p>After a few seconds, the <strong>&#8220;License Acceptance&#8221;<\/strong> dialog shows up. On this dialog, click <strong>&#8220;I Accept&#8221;.<\/strong><\/p>\n<p><strong>\n<a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5327.Accept20upgrade.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5327.Accept20upgrade.png\" alt=\"\" width=\"493\" height=\"331\" border=\"0\" \/><\/a>\n<\/strong><\/p>\n<p>It will update these assemblies for this project. Then &#8220;<strong>Close<\/strong>&#8221; the dialog when it is done.<\/p>\n<p>Check the OData core and client libraries assemblies, and it will show that you have upgraded them to the most recent version.<\/p>\n<h3><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5736.upgraded.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5736.upgraded.png\" alt=\"\" width=\"496\" height=\"521\" border=\"0\" \/><\/a><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/01\/63\/30\/1537.upgraded.PNG\">\n<\/a><\/h3>\n<p>&nbsp;<\/p>\n<h3>Appendix<\/h3>\n<p>The OData Code Generator V1.0.0 generates\u00a0two files different\u00a0from the V2.0.0\u00a0when adding new OData Client item template<\/p>\n<p>1. <strong><em>NorthwindProxy.odata.config<\/em><\/strong> contains all the configurations you need to set:<\/p>\n<ul>\n<li><strong>MetadataDocumentUri<\/strong><\/li>\n<\/ul>\n<blockquote><p>Service document URI or service metadata URI or a file path of metadata local copy.<\/p><\/blockquote>\n<ul>\n<li><strong>NamespacePrefix<\/strong><\/li>\n<\/ul>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>The value will be used as the namespace of your client proxy when metadata contains only one schema.<\/li>\n<li>The value will be added before namespaces of schemas when metadata contains several schemas.<\/li>\n<li>The namespace in metadata will be used if the value is empty string.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>2.<strong><em> NorthwindProxy.tt<\/em><\/strong> is the T4 template for generating proxy file from metadata document.<\/p>\n<p>Save your configuration file.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5430.Config_thumb_04C7C4CB.png\"><img decoding=\"async\" title=\"Config\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/5430.Config_thumb_04C7C4CB.png\" alt=\"Config\" width=\"625\" height=\"177\" border=\"0\" \/><\/a><\/p>\n<p>Re-trigger the code generation by right click at NorthwindProxy.tt, and select \u201c<strong>Run Custom Tool<\/strong>\u201d. The generated cs file will show in Solution Explorer under the tt file.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/4760.cs-Generated_thumb_23372716.png\"><img decoding=\"async\" title=\"cs Generated\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/4760.cs-Generated_thumb_23372716.png\" alt=\"cs Generated\" width=\"348\" height=\"323\" border=\"0\" \/><\/a><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/7144.cs-file_thumb_5F850D62.png\"><img decoding=\"async\" title=\"cs file\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/03\/7144.cs-file_thumb_5F850D62.png\" alt=\"cs file\" width=\"615\" height=\"341\" border=\"0\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Edited on\u00a0July 09, 2014 by updating \u201cAdd OData Client Proxy File\u201d for V2.0.0 and adding &#8220;Appendix&#8221; part. Please refer to &#8220;OData Client Code Generator 2.0.0 release&#8221; for V2.0.0 release notes Edited on Apr 29, 2014 by adding &#8220;Upgrade the\u00a0Project Dependencies to the Most Recent Version&#8221; part. In this tutorial, you will generate an OData client [&hellip;]<\/p>\n","protected":false},"author":518,"featured_media":3253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[23,50,71],"class_list":["post-413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odata","tag-data-services","tag-odata-client-tools","tag-t4"],"acf":[],"blog_post_summary":"<p>Edited on\u00a0July 09, 2014 by updating \u201cAdd OData Client Proxy File\u201d for V2.0.0 and adding &#8220;Appendix&#8221; part. Please refer to &#8220;OData Client Code Generator 2.0.0 release&#8221; for V2.0.0 release notes Edited on Apr 29, 2014 by adding &#8220;Upgrade the\u00a0Project Dependencies to the Most Recent Version&#8221; part. In this tutorial, you will generate an OData client [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/users\/518"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/comments?post=413"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/413\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media\/3253"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media?parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/categories?post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/tags?post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}