"
 
 
 
ASP.NET (snapshot 2017) Microsoft documentation and samples

Create an OData v4 Client App (C#)

by Mike Wasson

In the previous tutorial, you created a basic OData service that supports CRUD operations. Now let’s create a client for the service.

Start a new instance of Visual Studio and create a new console application project. In the New Project dialog, select Installed > Templates > Visual C# > Windows Desktop, and select the Console Application template. Name the project “ProductsApp”.

[!NOTE] You can also add the console app to the same Visual Studio solution that contains the OData service.

Install the OData Client Code Generator

From the Tools menu, select Extensions and Updates. Select Online > Visual Studio Gallery. In the search box, search for “OData Client Code Generator”. Click Download to install the VSIX. You might be prompted to restart Visual Studio.

Run the OData Service Locally

Run the ProductService project from Visual Studio. By default, Visual Studio launches a browser to the application root. Note the URI; you will need this in the next step. Leave the application running.

[!NOTE] If you put both projects in the same solution, make sure to run the ProductService project without debugging. In the next step, you will need to keep the service running while you modify the console application project.

Generate the Service Proxy

The service proxy is a .NET class that defines methods for accessing the OData service. The proxy translates method calls into HTTP requests. You will create the proxy class by running a T4 template.

Right-click the project. Select Add > New Item.

In the Add New Item dialog, select Visual C# Items > Code > OData Client. Name the template “ProductClient.tt”. Click Add and click through the security warning.

At this point, you’ll get an error, which you can ignore. Visual Studio automatically runs the template, but the template needs some configuration settings first.

Open the file ProductClient.odata.config. In the Parameter element, paste in the URI from the ProductService project (previous step). For example:

[!code-xmlMain]

   1:  <Parameter Name="MetadataDocumentUri" Value="http://localhost:61635/" />

Run the template again. In Solution Explorer, right click the ProductClient.tt file and select Run Custom Tool.

The template creates a code file named ProductClient.cs that defines the proxy. As you develop your app, if you change the OData endpoint, run the template again to update the proxy.

Use the Service Proxy to Call the OData Service

Open the file Program.cs and replace the boilerplate code with the following.

[!code-csharpMain]

   1:  using System;
   2:   
   3:  namespace ProductsApp
   4:  {
   5:      class Program
   6:      {
   7:          // Get an entire entity set.
   8:          static void ListAllProducts(Default.Container container)
   9:          {
  10:              foreach (var p in container.Products)
  11:              {
  12:                  Console.WriteLine("{0} {1} {2}", p.Name, p.Price, p.Category);
  13:              }
  14:          }
  15:   
  16:          static void AddProduct(Default.Container container, ProductService.Models.Product product)
  17:          {
  18:              container.AddToProducts(product);
  19:              var serviceResponse = container.SaveChanges();
  20:              foreach (var operationResponse in serviceResponse)
  21:              {
  22:                  Console.WriteLine("Response: {0}", operationResponse.StatusCode);
  23:              }
  24:          }
  25:   
  26:          static void Main(string[] args)
  27:          {
  28:              // TODO: Replace with your local URI.
  29:              string serviceUri = "http://localhost:port/";
  30:              var container = new Default.Container(new Uri(serviceUri));
  31:   
  32:              var product = new ProductService.Models.Product()
  33:              {
  34:                  Name = "Yo-yo",
  35:                  Category = "Toys",
  36:                  Price = 4.95M
  37:              };
  38:   
  39:              AddProduct(container, product);
  40:              ListAllProducts(container);
  41:          }
  42:      }
  43:  }

Replace the value of serviceUri with the service URI from earlier.

[!code-csharpMain]

   1:  // TODO: Replace with your local URI.
   2:  string serviceUri = "http://localhost:port/";

When you run the app, it should output the following:

[!code-consoleMain]

   1:  Response: 201
   2:  Yo-yo 4.95 Toys





Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-client-app.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>