In the book Exam Ref 70-487 page 82 is a code snippet :
String ServiceUri = "http://servicehost/ExamPrepService.svc";
ExamServiceContext ExamContext = new ExamServiceContext(new Uri(ServiceUri));
DataServiceQuery<Questions> = ExamContext.Question
.AddQueryOptions("$filter", "id gt 5")
.AddQueryOptions("$expand", "Answers");
but I want to see that it works!
A WCF dataservice consumed by a MVC 4 Web Application.
My steps :
I fallow the tutorial
Creating and Accessing a WCF Data Service in Visual Studio
Becareful! by step 8 from "To create the Entity Data Model", select all the tables, not just the customers table.
then
until 'Creating the Client Application'
then in place of a Windows Forms Application I will use a MVC 4 Web Application
- Right click on the Northwind solution, Add, New project, select Visual C#, ASP.NET MVC 4 Web Application.
- Name is NorthwindClient
- Select Project Template Empty
- Right click on References from the NorthwindClient project
- Add Service Reference
- Click on the button Discover, the service NorthwindCustomers.svc will be found
- Rename the namespace to ServiceReferenceNorthwind
- Build the project NorthwindClient to create the model class list
- Right click on the "Controllers" directory, Add, Controller, enter CustomersController in the controller field name.
- Template : Empty MVC controller, a simple controller file is created : CustomersController.cs
- Right click on the "Views" directory, Add, new Folder Customers
- Right click on the "Views/Customers" directory, Add, View, enter Index as View name
- Select "Create a strongly-typed view, in Model class select
- Customers (Test1.ServiceReferenceNorthwindCustomers)
- To create a quick list, select Scaffold template : List, click op Add button
- In the new Index.cshtml file, delete de following lines, because in this simple exemple we don't have implemented the CRUD methods
- Last step: in the file CustomersController.cs we will replace the Index action with
- @Html.ActionLink("Create New", "Create")
- @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
- @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
- @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
{
var serviceReferenceNorthwind = new ServiceReferenceNorthwind.NorthwindEntities(
new Uri("http://localhost:HERE-YOUR-PORT/NorthwindCustomers.svc"));
var customers = serviceReferenceNorthwind.Customers
.AddQueryOption("$top", "5")
.AddQueryOption("$orderby","ContactName")
.Execute();
.AddQueryOption("$top", "5")
.AddQueryOption("$orderby","ContactName")
.Execute();
return View(customers);
}
}
In a real application we will use a DAO to fetch the customers object.
Replace HERE-YOUR-PORT with the port-number where your service is running (see properties from your NorthwindWeb project)
You can notice that the syntax from the query option differ from the book and the controller-file: AddQueryOptions vs AddQueryOption (without s at the end)
I don't find AddQueryOptions in the documentation from Microsoft.
You can notice that the syntax from the query option differ from the book and the controller-file: AddQueryOptions vs AddQueryOption (without s at the end)
I don't find AddQueryOptions in the documentation from Microsoft.
see How to: Add Query Options to a Data Service Query (WCF Data Services)
- Right click on the solution, click on Properties, on the tab Startup Project,
- select Multiple startup projects
- NorthwindWeb = Start
- NorthwindClient = Start
- F5 start the applications
- In your browser go to http://localhost:HERE-YOUR-PORT/Customers
- The first 5 customers, order by contact name will be shown on a list.
Geen opmerkingen:
Een reactie posten