How to Consume a WCF Service Using WPF

Follow the steps to create a WCF service, an ADO.NET Entity Data Model, and a service contract, then consume the service using WPF

Related: "New Features in Windows Communication Foundation 4.0" and "Security Practices for WCF"

Download the Code

Windows Communication Foundation (WCF) is a framework that allows you to implement service-oriented applications using the CLR programming constructs of Microsoft .NET Framework. In essence, WCF is a service-oriented messaging system that offers you reliable, transacted services. It provides support for interoperable, reliable, secure communications through the usage of SOAP. Also, WCF promotes usage of a declarative and attribute-driven programming model; it provides a framework that enables applications to inter-communicate. In essence, WCF provides a service-oriented messaging system that offers you reliable, transacted services over SOAP. This article discusses how you can implement a WCF service and then consume it using a Windows Presentation Foundation (WPF) client.

The Prerequisites

To execute the code example illustrated in this article, you should have the following installed in your system:

  • Visual Studio 2008 with SP1 or Visual Studio 2010
  • ADO.NET Entity Framework 1.0
  • SQL Server 2008 or 2005
  • AdventureWorks database

Creating the WCF Service

To create a WCF Service in Visual Studio, follow these steps:
1. Open Visual Studio and click File, New, Web Site.
2. Select WCF Service Application from the list of the templates displayed, as Figure 1 shows.

3. Save the WCF service with a name.

The WCF Service will be created. You will then see two files in Solution Explorer, one named IService1.cs and the other, Service1.svc. I'll discuss these files in more detail later.

Creating the Data Model

We will use ADO.NET Entity Framework to create our data context. Before we delve more deeply into the implementation details, let's take a quick tour of Entity Framework. The ADO.NET Entity Framework (EF) is an extended ORM from Microsoft that you can use to query data from any data source. The basic objective of the EF is to objectify your application's data by adding a level of abstraction on top of the relational model and in doing so, raise the level of abstraction and isolate the logical model of your application's data from the relational model. To query data exposed through the EF, you can use Entity SQL, Object Services, or even strongly typed LINQ. The EF is an extended ORM because it provides support for entity inheritance, entity composition, change tracking, and so on—features that are not supported by the other ORMs of its kind.

According to MSDN, "A primary goal of the ADO.NET Entity Framework is to raise the level of abstraction available for data programming, thus simplifying the development of data aware applications and enabling developers to write less code. The Entity Framework is the evolution of ADO.NET that allows developers to program in terms of the standard ADO.NET abstraction or in terms of persistent objects (ORM) and is built upon the standard ADO.NET Provider model which allows access to third party databases."

We will now use the ADO.NET Entity Data Model Designer (this comes by default with Visual Studio 2010; you need to install Visual Studio 2008 SP1 if you're using Visual Studio 2008) to create our data model. To create an Entity Data Model using this designer, follow these steps:

1. Right-click the AdventureWorksService in Solution Explorer and click Add, New Item.
2. From the list of installed templates, select ADO.NET Entity Data Model, as Figure 2 shows.
 

3. Specify the name of the model and click Add.
4. In the Entity Data Model Wizard, specify the connection settings for the data model and click Next.
5. Now select the Department table from the list of the database objects, as shown in Figure 3.

6. Click Finish to generate the data model.

Figure 4 shows what our Entity Data Model looks like in the Model Browser.



There are three possible types of contracts that WCF supports: a service contract that defines the service endpoints and supported operations, a data contract that defines how a complex type can be serialized as part of a message in a WCF communication, and a message contract that defines the format of the complete SOAP message.

The next step is to create a service contract for this WCF service. To create a service contract, you have to define an interface containing a list of method signatures that represent the service operations. The service contract interface should have the ServiceContract attribute set with the OperationContract attribute in each of its methods, as shown in the following example:

using System.ServiceModel;
 \\[ServiceContract\\]
    public interface IService1
    \\{
   \\[OperationContract\\]
    List GetRecords();
    \\}

This IService1 interface would be implemented by the Service class called Service1. The Service class implements this interface and defines the GetRecords() method, as shown below.

public class Service1 : IService1
    \\{
    public List GetRecords()
    \\{
        AdventureWorksEntities dataContext = new AdventureWorksEntities();
        return dataContext.Departments.ToList();
    \\}
    \\}
Here is how the service is configured in the application's web.config file:

Compile the service and execute it to check that everything is working perfectly. If all is well, you can proceed with consuming the service using a WPF client.

Consuming the Service

We will consume the WCF service we just created using WPF. So, we'll first create a new WPF application. To do so, follow these steps:
1. Click File, New Project and select WPF Application from the list of the templates displayed, as shown in Figure 5.
 

2. Next, add the WCF service as a service reference, as Figure 6 shows.
 

Once you do this, the service information is placed in the app.config file, which looks like this:

At this point, Solution Explorer looks like Figure 7.

Designing the UI

The next step is to create a ListView control, which will be used to display the department names returned by the service method. Here's what the complete XAML markup code would look like:

Invoking the WCF Service

Now that the service reference has been added, you can easily access the WCF service from the WPF application. You just need to create a reference of your Service class, then invoke the method, as shown in the following example:

ServiceReference1.Service1Client myService = new ServiceReference1.Service1Client();lstView.ItemsSource = myService.GetRecords().Select(p => p.Name);

Here is the complete source code of the Windows1.xaml.cs file:

When you execute the WPF application, the output will look like that in Figure 8.
 

Getting into WCF

In this article we've looked at how to build a WCF service and then consume it using a WPF application. By following the steps I've provided in this article, you should be able to get started creating your own WCF services. For more articles about WCF, see articles by Michele Leroux Bustamante and Zoiner Tejada on Dev Pro.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish