Exercise 1: Creating a Service Agent Class

In this exercise you'll remove existing code in a code-beside file and create a service agent class. The service agent class will be used to make calls to a WCF service from within a ViewModel class created later in the lab.

  1. Open the following Visual Studio 2010 solution file based upon your chosen language.

    Language

    Lab Files Location

    C#

    /MVVMPattern/Starting Point/C#/CustomerViewer.sln

    Visual Basic

    /MVVMPattern/Starting Point/VB/CustomerViewer.sln

  2. The following projects are available in the solution:

    • CustomerService.Model – Contains entities and data repository classes used to access an AdventureWorks LT database.
    • CustomersService – A WCF service application that exposes entities to various applications.
    • SilverlightCustomerViewer – A Silverlight project that consumes data from a WCF service.
    • SilverlightCustomerViewer.Web – A website project used to host the SilverlightCustomerViewer application.

    Figure 1

    Projects in the Solution

  3. Right-click CustomerService.svc in the CustomersService project and select View in Browser from the menu. This will start a local WCF server and show details about the service.
  4. Back in Visual Studio, right-click SilverlightCustomerViewerTestPage.html in the SilverlightCustomerViewer.Web project and select View in Browser from the menu. Select customers from the ComboBox control (note that you may have to wait a moment for the customers to load the first time the application is run) and notice that the data for each customer displays in the details area.
  5. Back in Visual Studio, open MainPage.xaml.cs located in the SilverlightCustomerViewer project in the code editor and remove all of the existing code within the MainPage class except the constructor and the call to InitializeComponent.
    Note:
    All of the functionality in the code-beside file will be moved into a ViewModel class that you'll create later in this lab.
  6. Open MainPage.xaml and remove the Click event handlers defined on the two Button controls.
  7. Right-click on the SilverlightCustomerViewer project and select Add New Folder. Give the folder a name of ServiceAgents.
  8. Add a new class into the ServiceAgents folder named CustomersServiceAgent.
  9. Add the following interface immediately above the class and resolve any missing namespaces:
    Note:
    Resolve any missing namespaces by right-clicking on an unknown type and then selecting Resolve in the menu.

    C#

    public interface ICustomersServiceAgent { void GetCustomers(EventHandler<GetCustomersCompletedEventArgs> callback); void SaveCustomer(Customer cust, EventHandler<SaveCustomerCompletedEventArgs> callback); }

Visual Basic

Public InterfaceICustomersServiceAgent SubGetCustomers(ByValcallback As_ EventHandler(OfGetCustomersCompletedEventArgs)) SubSaveCustomer(ByValcustAs Customer,_ ByVal callback As EventHandler(Of SaveCustomerCompletedEventArgs))End Interface
  1. Implement the ICustomerService interface on the CustomersServiceAgent class.
  2. Add the following field into the CustomersServiceAgent class:

    C#

    CustomerServiceClient _Proxy = new CustomerServiceClient();

    Visual Basic

    Dim _Proxy as New CustomerServiceClient()
    Note:
    Note: The CustomerServiceClient class is a WCF service proxy class that will be used to communicate with the remote service to retrieve customer data and perform update and delete operations.
  3. Add the following code in the GetCustomers method to handle calling the WCF service operation:

    C#

    _Proxy.GetCustomersCompleted += callback; _Proxy.GetCustomersAsync();

    Visual Basic

    AddHandler _Proxy.GetCustomersCompleted, callback_Proxy.GetCustomersAsync()
  4. Add the following code in the SaveCustomer method to handle communicating delete and update data operations to the WCF service:

    C#

    _Proxy.SaveCustomerCompleted += callback; _Proxy.SaveCustomerAsync(cust);

    Visual Basic

    AddHandler _Proxy.SaveCustomerCompleted, callback _Proxy.SaveCustomerAsync(cust)