maandag 6 april 2015

70-487 WCF (Windows Communication Foundation) Web Services

WCF (Windows Communication Foundation) on wiki

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


  1. Right click on the Northwind solution, Add, New project, select Visual C#, ASP.NET MVC 4 Web Application. 
    1. Name is NorthwindClient
    2. Select Project Template Empty
  2. Right click on References from the NorthwindClient project
    1. Add Service Reference
    2. Click on the button Discover, the service NorthwindCustomers.svc will be found
    3. Rename the namespace to ServiceReferenceNorthwind
  3. Build the project NorthwindClient to create the model class list
  4. Right click on the "Controllers" directory, Add, Controller, enter CustomersController in the controller field name.
    1. Template : Empty MVC controller, a simple controller file is created : CustomersController.cs
  5. Right click on the "Views" directory, Add, new Folder Customers
  6. Right click on the "Views/Customers" directory, Add, View, enter Index as View name
  7. Select "Create a strongly-typed view, in Model class select 
    1. Customers (Test1.ServiceReferenceNorthwindCustomers)
    2. To create a quick list, select Scaffold template : List, click op Add button
    3. In the new Index.cshtml file, delete de following lines, because in this simple exemple we don't have implemented the CRUD methods

      1. @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 */ })

  8. Last step: in the file CustomersController.cs we will replace the Index action with
public ActionResult Index() 
 { 
 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(); 


 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.
 see How to: Add Query Options to a Data Service Query (WCF Data Services)

      Now we will check the result

  1. Right click on the solution, click on Properties, on the tab Startup Project, 
    1. select Multiple startup projects
    2. NorthwindWeb = Start
    3. NorthwindClient = Start
  2. F5 start the applications
  3. In your browser go to http://localhost:HERE-YOUR-PORT/Customers
  4. The first 5 customers, order by contact name will be shown on a list.

Geen opmerkingen:

Een reactie posten