Webapi Learning Note 08: Console Application Templates--Creating an OData client application

Source: Internet
Author: User

1. Create a project

Right-click OData folder, add-"New project:

2. Installing the OData Client code generator

Open vs, Tools-"extensions and updates:

3. Build the Service Proxy

Under the root directory, add:

Now you need to open a different vs and run the Productservice project (the example from the previous chapter).

Modify the Productclient.tt and fill in the URI with the URL of the Productservice project run:

Click Save, no error prompts, you can generate code, stating OK. Such as:

4. Invoking the OData service by using a service proxy

Modify the Program.cs as follows:

usingSystem;namespaceproductapp{classProgram {//Get an entire entity set.        Static voidlistallproducts (Default.container Container) {foreach(varPinchcontainer. Products) {Console.WriteLine ("{0} {1} {2}", P.name, P.price, p.category); }        }        Static voidaddproduct (Default.container Container, ProductService.Models.Product Product) {Container.            Addtoproducts (product); varServiceresponse =container.            SaveChanges (); foreach(varOperationresponseinchserviceresponse) {Console.WriteLine ("Response: {0}", Operationresponse.statuscode); }        }        Static voidMain (string[] args) {            stringServiceuri ="http://localhost:52848/"; varcontainer =NewDefault.container (NewUri (Serviceuri)); varProduct =NewProductService.Models.Product () {Name="Yo-Yo", Category="Toys", Price=4.95M            };            Addproduct (container, product);            Listallproducts (container);        Console.readkey (); }    }}

Operation Result:

Webapi Learning Note 08: Console Application Templates--Creating an OData client application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.