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