Exercise 1: Creating a Silverlight-Enabled WCF Service
In this exercise you'll create a new Silverlight Navigation Application and add a Silverlight-Enabled WCF Service to the Web project. You'll then add code into the service's operations to make calls to repository objects responsible for communicating with a database using Entity Framework 4.
Create a new Silverlight Navigation Application
- Create a new Silverlight Navigation Application in Visual Studio 2010 named UsingWCFServices.
- Right-click on the UsingWCFService.Web project and select Add Add ASP.NET Folder App_Data.
- Right-click on the App_Data folder and select Add Existing Item from the menu. Add the following file:
- WCFServices/Starting Point/AdventureWorksLT_Data.mdf
- Add two new folders into UsingWCFServices.Web named Models and Services.
- Add a new ADO.NET Entity Data Model into the Models folder named AdventureWorksLT.edmx.
The ADO.NET Entity Data Model template can be found in the Data section of the Add New Item dialog. Alternatively, you can use Visual Studio 2010's Search Installed Templates feature in the upper-right corner of the dialog window to search for the template as well.
- Select Generate from database from the options and click the Next button.
.png)
Figure 1Entity Framework Wizard
- Ensure that AdventureWorksLT_Data.mdf is selected in the drop-down list and click Next.
- Expand the Tables node and select the Customer and SalesOrderHeader tables and click Finish.
.png)
- Right-click on the Services folder and add a new Silverlight-enabled WCF Service into it. Name the service CustomersService.svc.
The Silverlight-enabled WCF Service template is located in the Silverlight templates section that's available when adding new items into a project.
- Take a moment to examine the existing DoWork method and notice the OperationContract attribute above it. This attribute is used to mark the method as a service operation that can be called from distributed applications. Delete the DoWork method as well as its OperationContract attribute from the class.
Although you can add code logic directly into WCF service methods (often referred to as "operations"), it's recommended that you rely on external classes to handle business rules, interact with data access frameworks, etc. Rather than adding data access code into the service operations you'll rely on a set of data classes named CustomerRepository and SalesOrderHeaderRepository to perform the work in this lab.
- Open web.config and locate the system.serviceModel element. Notice that a custom binding has been added (locate the customBinding element) that uses HTTP and binary message encoding. This combination provides excellent performance when exchanging data between a client and a service.
- Right-click on the UsingWCFServices.Web/Models folder and select Add Existing Item. Add all of the code files found in the folder shown next into the Models folder:
Language | File Location |
|---|
C# | WCFServices/Starting Point/C# |
VB | WCFServices/Starting Point/VB |
- Open the CustomerRepository and SalesOrderHeaderRepository classes in the Models folder and take a moment to look through their code
These classes derive from a custom RepositoryBase class and contain functionality to perform different database operations.
- Open the OperationStatus class and note that it's used to return status information about different operations that occur in the repository classes.
- Add the following code into the CustomersService class and resolve any missing namespaces:
ICustomerRepository _CustomerRepository = new CustomerRepository();ISalesOrderHeaderRepository _OrderRepository = new SalesOrderHeaderRepository();
Dim _CustomerRepository As ICustomerRepository = New CustomerRepository()Dim _OrderRepository As ISalesOrderHeaderRepository = _ New SalesOrderHeaderRepository()
Although this code defines the repository class type to use in the CustomersService class, because each repository class implements an interface the type could be injected into the service at runtime. This is useful in situations where more loosely coupled code is needed.
- Add the following public methods and associated parameters into the CustomersService class and resolve any missing namespaces:
Method | Return Type | Parameters |
|---|
GetCustomers | List of Customer | None |
GetOrdersByCustomerID | List of SalesOrderHeader | Integer named customerID |
UpdateSalesOrderHeader | OperationStatus | SalesOrderHeader named order |
- Add the OperationContract attribute above each of the methods.
- Add code into GetCustomers to call the _CustomerRepository.GetCustomers method:
return _CustomerRepository.GetCustomers();
return _CustomerRepository.GetCustomers()
- Add code into GetOrdersByCustomerID to call the _OrderRepository.GetOrdersByCustomerID method and return a collection. Pass the service operation's customerID parameter to the repository object's method.
- Add code into UpdateSalesOrderHeader to call the _OrderRepository.UpdateSalesOrderHeader method and return an OperationStatus object. Pass the service operation's order parameter to the repository object's method.
- Build the solution and resolve any compilation issues before continuing.
- Right-click the CustomersService.svc file in the Solution Explorer and select View in Browser. You should see a service test page appear.