Using WCF 3.5 REST Services and Consuming it in WPF 3.5 applications - An Application Development approach.

Abstract: WCF REST based services is one of the most important features/usages of WCF services. In this article, we will create a WCF REST service that contains various methods to read data from a database server. The WPF client application will work as dash-board and consume this service.

WCF REST based services is one of the most important features/usages of WCF services. WCF REST is also known as Web style WCF services. WCF REST outputs in Plain Old (POX) form. One nice feature of WCF REST is that now any client application which understands XML, can send a request to WCF services. If you have developed applications using WCF 3.0, you must have added the service reference of WCF service in client applications. This creates a proxy class and configuration file. Client application makes request to WCF service using this proxy class and information from configuration file. In this case now if the interface is changed, then the service reference needs to be updated. But if WCF REST is used, then the client application can directly make a call to WCF service using repository (URL) and the operation (Operationcontract). For WCF REST programming ‘Syatem.ServiceModel.Web’, namespace is provided. This namespace provides the ‘WebGet’ and ‘WebInvoke’ classes. ‘WebGet’ helps to retrieve data from the WCF service in the form of XML. ‘WebInvoke’ is used to perform ‘POST’,’PUT’ and ‘DELETE’ HTTP calls.

In this article, we will create a WCF REST service that contains various methods to read data from a database server. The WPF client application will work as dash-board and consume this service. The client application will output as below:

Consider that you are working for the end user client and this client wants the UI as shown above. In this case, the data is coming from a database and the client is expecting a loosely coupled service communication over http.The Client application need not have any proxy class and any endpoint knowledge. If the UI application were to be changed in future, the proxy should not to be created again. To make this possible, WCF REST can be the best solution in this case.

Task 1: Database Table creation.

Step 1: Create database ‘Company’.

Step 2: Add the following tables with columns. The description of each table is given below the image:

The above table is used to store Employee information.

The above table is used to store company wise quarter wise sales information.

The above table is used to store ItemName wise sales quantity data.

The above table is used to store state wise sales quantity.

Task 2: Creating DataContract.

Step 1: Open VS 2008 and create a blank solution, name this as ‘WPF_WCFRESTApplication’.

Step 2: In this project add a class library project, name this as ‘clsDataContracts’. Rename ‘Class1.cs’ to ‘clsEmployee.cs’. In this project, add reference for ‘System.Runtime.Serialization’.

Step 3: Add following ‘DataContract’ classes:

C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.Serialization;

namespace clsDataContracts

{

[DataContract]

publicclassclsEmployee

{

[DataMember]

publicint EmpNo { get; set; }

[DataMember]

publicstring EmpName { get; set; }

[DataMember]

publicint Salary { get; set; }

[DataMember]

publicint DeptNo { get; set; }

}

[DataContract]

publicclassclsSales

{

[DataMember]

publicint CompanyId { get; set; }

[DataMember]

publicstring CompanyName { get; set; }

[DataMember]

publicdecimal Q1 { get; set; }

[DataMember]

publicdecimal Q2 { get; set; }

[DataMember]

publicdecimal Q3 { get; set; }

[DataMember]

publicdecimal Q4 { get; set; }

}

[DataContract]

publicclassclsSalesData

{

[DataMember]

publicstring ItemName { get; set; }

[DataMember]

publicint SalesQty { get; set; }

}

[DataContract]

publicclassclsStatewiseSales

{

[DataMember]

publicstring StateName { get; set; }

[DataMember]

publicdecimal SalesQuantity { get; set; }

}

}

VB.NET

Imports System

Imports System.Collections.Generic

Imports System.Linq

Imports System.Text

Imports System.Runtime.Serialization

Namespace clsDataContracts

<DataContract> _

PublicClass clsEmployee

Private privateEmpNo AsInteger

<DataMember> _

PublicProperty EmpNo() AsInteger

Get

Return privateEmpNo

EndGet

Set(ByVal value AsInteger)

privateEmpNo = value

EndSet

EndProperty

Private privateEmpName AsString

<DataMember> _

PublicProperty EmpName() AsString

Get

Return privateEmpName

EndGet

Set(ByVal value AsString)

privateEmpName = value

EndSet

EndProperty

Private privateSalary AsInteger

<DataMember> _

PublicProperty Salary() AsInteger

Get

Return privateSalary

EndGet

Set(ByVal value AsInteger)

privateSalary = value

EndSet

EndProperty

Private privateDeptNo AsInteger

<DataMember> _

PublicProperty DeptNo() AsInteger

Get

Return privateDeptNo

EndGet

Set(ByVal value AsInteger)

privateDeptNo = value

EndSet

EndProperty

EndClass

<DataContract> _

PublicClass clsSales

Private privateCompanyId AsInteger

<DataMember> _

PublicProperty CompanyId() AsInteger

Get

Return privateCompanyId

EndGet

Set(ByVal value AsInteger)

privateCompanyId = value

EndSet

EndProperty

Private privateCompanyName AsString

<DataMember> _

PublicProperty CompanyName() AsString

Get

Return privateCompanyName

EndGet

Set(ByVal value AsString)

privateCompanyName = value

EndSet

EndProperty

Private privateQ1 AsDecimal

<DataMember> _

PublicProperty Q1() AsDecimal

Get

Return privateQ1

EndGet

Set(ByVal value AsDecimal)

privateQ1 = value

EndSet

EndProperty

Private privateQ2 AsDecimal

<DataMember> _

PublicProperty Q2() AsDecimal

Get

Return privateQ2

EndGet

Set(ByVal value AsDecimal)

privateQ2 = value

EndSet

EndProperty

Private privateQ3 AsDecimal

<DataMember> _

PublicProperty Q3() AsDecimal

Get

Return privateQ3

EndGet

Set(ByVal value AsDecimal)

privateQ3 = value

EndSet

EndProperty

Private privateQ4 AsDecimal

<DataMember> _

PublicProperty Q4() AsDecimal

Get

Return privateQ4

EndGet

Set(ByVal value AsDecimal)

privateQ4 = value

EndSet

EndProperty

EndClass

<DataContract> _

PublicClass clsSalesData

Private privateItemName AsString

<DataMember> _

PublicProperty ItemName() AsString

Get

Return privateItemName

EndGet

Set(ByVal value AsString)

privateItemName = value

EndSet

EndProperty

Private privateSalesQty AsInteger

<DataMember> _

PublicProperty SalesQty() AsInteger

Get

Return privateSalesQty

EndGet

Set(ByVal value AsInteger)

privateSalesQty = value

EndSet

EndProperty

EndClass

<DataContract> _

PublicClass clsStatewiseSales

Private privateStateName AsString

<DataMember> _

PublicProperty StateName() AsString

Get

Return privateStateName

EndGet

Set(ByVal value AsString)

privateStateName = value

EndSet

EndProperty

Private privateSalesQuantity AsDecimal

<DataMember> _

PublicProperty SalesQuantity() AsDecimal

Get

Return privateSalesQuantity

EndGet

Set(ByVal value AsDecimal)

privateSalesQuantity = value

EndSet

EndProperty

EndClass

EndNamespace

All the above classes provide property mapping with data tables on database server.

Task 3: Creating WCF REST Service.

Step 1: To the solution, add a new WCF Service Application project from ‘Web Project Types'. Name this as ‘WCF_Service’. In this project, add a reference to ‘System.ServiceModel.Web’. Rename ‘IService1.cs’ to ‘IService.cs’. Rename ‘Service1.svc’ to ‘CService.svc’.

Step 2: Right click on ‘CService.svc’ and select ‘View Markup’ and make following changes:

In the above xaml, the grid is divided into two rows and two clients. These contains various graphs and ListView. (Note:When you drag-drop Graph controls, you get a reference to ‘System.Windows.Controls.DataVisualization.Toolkit. If not, add the reference explicitly and add ’xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;

Step 5: Run the application and the following output will be displayed.

Conclusion:

WCF REST services need not to added as a service reference to client applications. This provides loosely coupled behavior to the client application. The client application, which understand XML can make request to the WCF service using HTTP. WCF REST can be used in JavaScript, PHP, AJAX etc.

Mahesh is having 10 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). Follow him on twitter @maheshdotnet