{"id":3766,"date":"2020-03-06T11:08:29","date_gmt":"2020-03-06T18:08:29","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/odata\/?p=3766"},"modified":"2020-03-09T00:27:21","modified_gmt":"2020-03-09T07:27:21","slug":"odata-connected-service-0-4-0-release","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/odata\/odata-connected-service-0-4-0-release\/","title":{"rendered":"OData Connected Service 0.4.0 Release"},"content":{"rendered":"<p><span data-contrast=\"auto\">OData Connected Service 0.4.0 has been released and is now available on the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=laylaliu.ODataConnectedService\">Visual Studio Marketplace<\/a>.<\/span><\/p>\n<p><span data-contrast=\"auto\">T<\/span><span data-contrast=\"auto\">he new version adds the following features:<\/span><\/p>\n<ol>\n<li>Support for Visual Studio 2019 (in addition to Visual Studio 2017)<\/li>\n<li>Option to generate types as internal so that they are not accessible outside the assembly<\/li>\n<li>Bug fixes<\/li>\n<\/ol>\n<p>In this article, I would like to take you through some key new features and get you up to speed with using the OData Connected Service.<\/p>\n<h2><span class=\"TextRun BCX0 SCXW143866224\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun BCX0 SCXW143866224\" data-ccp-parastyle=\"heading 1\" data-wac-het=\"1\">OData Connected Service in Visual Studio 2019<\/span><\/span><\/h2>\n<p><span data-contrast=\"auto\">We start by illustrating how you can use the extension in Visual Studio 2019. Open Visual Studio 2019 and create a new C# <\/span><span data-contrast=\"auto\">.Net<\/span><span data-contrast=\"auto\">\u00a0Core Console project.<\/span><span data-contrast=\"auto\"> Let\u2019s call the project &#8220;<\/span><span data-contrast=\"auto\">SampleClient&#8221;<\/span><span data-contrast=\"auto\">, and the solution &#8220;<\/span><span data-contrast=\"auto\">OCSTest&#8221;<\/span><span data-contrast=\"auto\">.<\/span><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">Once the project is open, click the Extensions menu, then\u00a0<\/span><span data-contrast=\"auto\">Manage Extensions. In the Manage Extensions window, search for \u201cOData Connected Service\u201d<\/span><span data-contrast=\"auto\">. Select the extension and install it.<\/span><span data-contrast=\"auto\">\u00a0You may need to close Visual Studio to allow the extension to install, then restart it after installation\u00a0<\/span><span data-contrast=\"auto\">completes.<\/span><\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-3774\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/OCS-0.4.0-Extensions-Download.png\" alt=\"Image OCS 0 4 0 Extensions Download\" width=\"846\" height=\"218\" \/><\/p>\n<p>Alternatively, you can just <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=laylaliu.ODataConnectedService\">download the VSIX from the marketplace<\/a> and double click to install.<\/p>\n<p>Once the extension has been installed. Right-click your project in the solution explorer, then in the context menu select <strong>Add<\/strong> &gt; <strong>Connected Service<\/strong>. This will open the<strong> Connected Services<\/strong> window where you can select which services to add to your project. Select <strong>OData Connected Service<\/strong>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-medium wp-image-3767\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Add-Connected-Service-Menu-300x227.png\" alt=\"Image Add Connected Service Menu\" width=\"300\" height=\"227\" srcset=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Add-Connected-Service-Menu-300x227.png 300w, https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Add-Connected-Service-Menu.png 674w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><img decoding=\"async\" class=\"alignnone size-medium wp-image-3771\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Connected-Services-list-Add-OCS-300x156.png\" alt=\"Image Connected Services list 8211 Add OCS\" width=\"300\" height=\"156\" srcset=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Connected-Services-list-Add-OCS-300x156.png 300w, https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Connected-Services-list-Add-OCS.png 685w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Next, a configuration wizard will open where you can configure how code will be generated for your service.<\/p>\n<p>On the first page, we\u2019ll add the metadata URL of the service we want to access. For this example, we will use the sample Trip Pin Service. Set the service name to \u201cTripPin Service\u201d and the Address to \u00a0https:\/\/services.odata.org\/TripPinRESTierService\/$metadata<\/p>\n<p>Then click <strong>Finish<\/strong>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-medium wp-image-3769\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Configure-OCS-Endpoint-300x206.png\" alt=\"Image Configure OCS Endpoint\" width=\"300\" height=\"206\" srcset=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Configure-OCS-Endpoint-300x206.png 300w, https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Configure-OCS-Endpoint-768x527.png 768w, https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Configure-OCS-Endpoint.png 802w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>After the process completes, the OData docs website will be launched. Go back to Visual Studio and you will see a connected service added to your project and a <strong>Reference.cs<\/strong> file that \u00a0contains all the code generated from the OData code generator.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-3770\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Connected-Service-Added-to-Project.png\" alt=\"Image Connected Service Added to Project\" width=\"235\" height=\"168\" \/><\/p>\n<p>Let&#8217;s proceed to use the generate classes to interact with the service. Replace the code in <strong>Program.cs<\/strong> with the following:<\/p>\n<pre class=\"lang:c# decode:true\" title=\"Display list of people from an OData Service using the generated client code.\">using System; \r\nusing System.Threading.Tasks;\r\nusing Microsoft.OData.Service.Sample.TrippinInMemory.Models;\r\n\r\nclass Program\r\n{\r\n    static string serviceUri = \"https:\/\/services.odata.org\/TripPinRESTierService\/\";\r\n    static Container context = new Container(new Uri(serviceUri));\r\n    static void Main()\r\n    {\r\n        \/\/ from C# 7.1 You can use async Main instead\r\n        ShowPeople().Wait();\r\n    }\r\n\r\n    static async Task ShowPeople()\r\n    {\r\n        var people = await context.People.ExecuteAsync();\r\n        foreach (var person in people)\r\n        {\r\n            Console.WriteLine(person.FirstName);\r\n        }\r\n    }\r\n}<\/pre>\n<p>The above program fetches people data from the Trip Pin service and then displays their names. If you run the program, it should display a list of names.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-3780\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/OData-Connected-Service-app-sample-people-output.png\" alt=\"Image OData Connected Service app sample people output\" width=\"907\" height=\"255\" \/><\/p>\n<p>To learn more about using the generated client to interact with an OData service, <a href=\"https:\/\/docs.microsoft.com\/en-us\/odata\/client\/basic-crud-operations\">visit this guide<\/a>.<\/p>\n<h2>Generating types as internal<\/h2>\n<p>This feature allows you to mark generated types as internal instead of public, so that they are not accessible outside your assembly. This helps avoid conflicts if you&#8217;re referencing different projects that use the same classes generated by OData Connected Service from the same service endpoint.<\/p>\n<p>To illustrate this,\u00a0 add a library project to our OCSTest solution. Right-click the solution and click <strong>Add<\/strong> &gt; <strong>New Project<\/strong>. Create a C# .Net Standard Class Library project and call it SampleLib. This library will expose a simple method that makes use of the same Trip Pin service we used earlier.<\/p>\n<p>Follow the same steps as in the previous section to add an OData Connected Service to this project using the same Trip Pin service metadata endpoint.<\/p>\n<p>Next,\u00a0 add a public class to our library project that will contain the method we want to expose to consumers of the library. Name the file <strong>LibService.cs <\/strong>\u00a0and add the following code to the file:<\/p>\n<pre class=\"lang:c# decode:true\" title=\"Library code exposing a method to get the most popular person\">using System;\r\nusing System.Threading.Tasks;\r\nusing Microsoft.OData.Service.Sample.TrippinInMemory.Models;\r\n\r\nnamespace SampleLib\r\n{\r\n    public static class LibService\r\n    {\r\n        private static string serviceUri = \"https:\/\/services.odata.org\/TripPinRESTierService\/\";\r\n        private static Container context = new Container(new Uri(serviceUri));\r\n\r\n        public static async Task&lt;string&gt; GetMostPopularPerson()\r\n        {\r\n            var person = await context.GetPersonWithMostFriends().GetValueAsync();\r\n            return $\"{person.FirstName} {person.LastName}\";\r\n        }\r\n    }\r\n}<\/pre>\n<p>The <code>GetMostPopularPerson()<\/code> method simply fetches the person with the most friends from the service and returns that person\u2019s full name.<\/p>\n<p>Let\u2019s add a reference to SampleLib in our initial console applicaton so that we can use it. Right click the SampleClient project &gt; <strong>Add<\/strong> &gt; <strong>Reference<\/strong> &gt; <strong>Projects<\/strong> &gt; <strong>Solution<\/strong> &gt; <strong>SampleLib<\/strong> then press <strong>OK<\/strong>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-3768\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Add-SampleLib-reference.png\" alt=\"Image Add SampleLib reference\" width=\"589\" height=\"366\" \/><\/p>\n<p>At this point, we can call the method from our SampleLib library in the Main method of the SampleClient console app, by adding the following line at the end of the Main method. The Main method should now look like:<\/p>\n<pre class=\"lang:c# decode:true\" title=\"Main method calls the library method to display the most popular person\">static void Main()\r\n{\r\n    \/\/ from C# 7.1 You can use async Main instead\r\n    ShowPeople().Wait();\r\n    Console.WriteLine(\"Most popular: {0}\", SampleLib.LibService.GetMostPopularPerson().Result);\r\n}<\/pre>\n<p>If you try to run this application, you will get compiler warnings on the Container class, because both the console app and library define classes with the same names in the same namespace. The console app has access to all the proxy classes of the library because they are public. But this is not what we want, we only want to expose the <code>LibService.GetMostPopularPerson()<\/code> method from the service, the proxy classes should not be accessible outside the library.<\/p>\n<p>To correct this issue, go to the SampleLib project in the solution explorer, under the Connected Services node, you will see a folder for the connected service you added, which is <strong>TripPin Service<\/strong>\u00a0in our example. Right click the folder and select <strong>Update Connected Service<\/strong>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-3775\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Update-OCS-Menu.png\" alt=\"Image Update OCS Menu\" width=\"336\" height=\"119\" srcset=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Update-OCS-Menu.png 336w, https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Update-OCS-Menu-300x106.png 300w\" sizes=\"(max-width: 336px) 100vw, 336px\" \/><\/p>\n<p>This will open up the OData Connected Wizard and allow you to update the configuration. On the Endpoint page click <strong>Next<\/strong> to navigate to the Settings page, then click the <strong>AdvancedSettings<\/strong> link to reveal more options. Check the <strong>Mark generated types as internal<\/strong> checkbox and click <strong>Finish<\/strong>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-3773\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Make-types-internal-OCS-option.png\" alt=\"Image Make types internal OCS option\" width=\"591\" height=\"349\" \/><\/p>\n<p>When asked whether to replace the existing Reference.cs file, click Yes. After the code generation is complete, you can open the generated Reference.cs file and confirm that all top-level classes and enums have an <code>internal<\/code>\u00a0access modifier.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-3772\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/Generated-code-with-internal-modifier.png\" alt=\"Image Generated code with internal modifier\" width=\"757\" height=\"285\" \/><\/p>\n<p>Finally, build and run the SampleClient project, you will not see the warnings again. The program will display the name of the person with most friends at the end.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-3782\" src=\"https:\/\/devblogs.microsoft.com\/odata\/wp-content\/uploads\/sites\/23\/2020\/03\/OData-Connected-Service-app-sample-output-with-most-popular-person.png\" alt=\"Image OData Connected Service app sample output with most popular person\" width=\"332\" height=\"331\" \/><\/p>\n<h2>Minor updates and bug fixes<\/h2>\n<p>The <code>ByKey<\/code> method now accepts an <code>IDictionary<\/code> as a parameter as opposed to the concrete Dictionary class that it allowed before. This allows you to pass your own implementation of <code>IDictionary<\/code> instead of the standard Dictionary when you need to.<\/p>\n<p>In addition, references to <code>EdmxReader<\/code> have been replaced with <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.odata.edm.csdl.csdlreader?view=odata-edm-7.0\">Microsoft.<wbr \/>OData.<wbr \/>Edm.<wbr \/>Csdl.CsdlReader<\/a>. This fixes some of the compilation errors that occurred in the generated code in the previous version.<\/p>\n<p>&nbsp;<\/p>\n<p>There are more features and fixes coming to OData Connected Service soon, so stay tuned for upcoming releases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OData Connected Service 0.4.0 has been released and is now available on the Visual Studio Marketplace. The new version adds the following features: Support for Visual Studio 2019 (in addition to Visual Studio 2017) Option to generate types as internal so that they are not accessible outside the assembly Bug fixes In this article, I [&hellip;]<\/p>\n","protected":false},"author":20326,"featured_media":3253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[118,1],"tags":[],"class_list":["post-3766","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-clientcodegen","category-odata"],"acf":[],"blog_post_summary":"<p>OData Connected Service 0.4.0 has been released and is now available on the Visual Studio Marketplace. The new version adds the following features: Support for Visual Studio 2019 (in addition to Visual Studio 2017) Option to generate types as internal so that they are not accessible outside the assembly Bug fixes In this article, I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/3766","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\/20326"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/comments?post=3766"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/3766\/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=3766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/categories?post=3766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/tags?post=3766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}