Published: 02 Apr 2008
By: Josip Zohil
Download Sample Code

This article explains how to set up a WCF client to download the data from the server in asynchronous (non blocking) mode, much faster than with VFP functions or oledb data adapters.

Introduction

Windows Communication Foundation (WCF) enables us to create distributed architectures and use Visual FoxPro (VFP) as a client/server system executing the operation on the server (backend). With the WCF client we can download the data from the server in asynchronous (no blocking) mode. The measurements in this article show us that this can be done much faster than downloading the data with VFP functions or oledb data adapters. With WCF, we can take advantage of parallel (asynchronous) operations and distribute the processing of data between the client and the server. The retrieved data can be presented in VFP or .NET (Windows) controls. In the sample code of this article you will find the code for four projects: WCF service, WCF client, COM object and VFP client. We are going to present the code and measure the retrieving of the data from the server in the form of a data table, a dataset, a strong typed collection and the dbf file. The fastest approach is the retrieve and transport of the dbf file we bind to the VFP grid. On the second place (24% slower) is the strong typed .NET collection we bind to the Net grid. The laziest (30% slower) is the .NET data table that we bind to the Windows Net grid. Except for very small record sets, there is a big time gap (almost 100%) between the direct VFP data retrieve over the local network with respect to the WCF service.

The problem

For example, from a table the size of 400 MB let us download six thousand records (four fields of total length less than 50 characters) from the VFP database on the server. The first retrieval of these records from the workstation can exceed 12 seconds (at the server only 1 second [6]). The workstation running the retrieval blocks for 12 seconds. If we download consequently two record sets, we block the VFP form for 24 seconds and so on. How can we retrieve two thousand large records set in few seconds without blocking the client?

The solution

On the first retrieve of data from the server the VFP client downloads also the relatively large index file and dbf table headers. Using WCF, we can retrieve the data on the server (backend) and download only the selected records. We can asynchronously execute the client operations (in parallel with other VFP operations). This may be the way to go if you have a VFP application running long VFP requests. You create and install:

  • WCF service. (How to create a WCF in Visual Studio 2008? see. [2]).We shall use the net.tcp protocol.
  • Host the WCF service. In our projects, we shall host it in Windows service. Create a Windows service and install it on the server.
  • Create the WCF client (How to do this? See [1], [3]).
  • Create the Windows control (Active-X) and make it COM visible (see [1]). Register it on the workstation.
  • Create the VFP project, the VFP form and add it to the created Active-X control. Run the VFP application on the workstation.

The components of the above projects are interconnected. We pass the data (objects) between these components. We shall pay special attention to the methods and objects we use for this intra components transport.

The WCF service

The main goal of the WCF service is to accept the requests from the client and pass the requests to the VFP backend; catch the response from the VFP database application (on the server) and pass the response to the client. We shall create the WCF service with a project named WfcVfpWsTcp and six files:

1. Service.cs (Listing 1). This file contain the interface IService1 with 6 operations which allows us to transport the .NET generic data types: String, DataSet, DataTable, Byte and a generated data type named OrderListCollection. In the background the WCF serializes these five objects. In all the methods we have the out parameter retError. With it, we send to the client the error message generated on the server (service) side. The service operation methods accept the names and parameters of the method it will execute in the VFP database and route it to the VFP database by calling VfpServerCon. Normally the service WfcVfpWsTcp is on the same computer as the VFP server.

Listing 1: The service.cs class

2. VfpServerCon.cs (Listing 2) (Its main goal is to retrieve the data from the VFP database using the OleDb driver). It accepts the names and parameters of the method it will execute in the VFP database, accept the response from the VFP (normally as the OleDbDataReader), transform the data (for example in byte array) and pass the data to the service. (You can retrieve the data from the VFP database also with the VFP COM object (see [3])).

Listing 2: The VfpServerCon.cs class

When we initialize the VfpServerCon class, it reads the connection string from the app.config file (see the key connString in app.config).

You have to write your own connection string in the app.config and write your own store procedures. In Listing 2 [6] is the stored procedure used in this article. It accepts two parameters and returns a result set. With small modifications, this procedure can write the results in the dbf file and return a string with its name. We suppose the last method is the fastest retrieve method. Transporting a dbf file is relatively unusual in WCF project, so we describe this process in more detail.

When we call the StoreFile method of the VfpServerCon instance (Listing 2), the StoreFile method is executed. The VFP database method StoreFile retrieves the data from the database, copies it in the dbf file and passes its name back to the calling method. It reads the dbf file from the disk and writes it in the byte buffer (Listing 2). The program passes this buffer to the service, which sends it to the client (Listing 1).

Listing 3: The OrderListCollection class

3. OrderListCollection.cs (Listing 3). We frame the elements of this class with DataContract, DataMember, CollectionDataContract and Serializable attributes. Do not forget to add to the service.cs the attribute [ServiceKnownType(typeof(Orders))]. Decorated with this attributes the new data type can be serialized with the WCF serialization tools. If you change any of this attribute, you have to update the client with the new service metadata.

Listing 4: The app.config file

4. app.config (Listing 4). In the app.config we put a net.tcp binding, its endpoints and the corresponding mex binding, which allow us to download the service description to the client.

We use the key filePath (Listing 4) in the method StoreFile (Listing 2). It passes to the VFP database method the path of the file written on the disk.

Do not forget to change this filePath to your database path. Change the base endpoint address (192.168.123.63) to the IP of your computer, where the service is installed.

Listing 5: The HostServiceVfp file

5. HostServiceVfp.cs (Listing 5). The class to start and stop the service. In case of a fault of the service, it writes the error message to the log file.

6. ProjectInstaller.cs. Enables the installation of the Windows service and makes it visible in the MMC (Administrative Tools - Services; see [5]). It creates the setup project WfcVfpWsTcpIn (see [4]).

About Josip Zohil

Josip Zohil, Koper, Slovenia, Josip.Zohil1@guest.arnes.si

This author has published 3 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Improve VFP data retrieval with WCF - Part Two
This article explains how to set up a WCF client to download the data from the server in asynchronou...
Improve VFP data retrieval with WCF - Part Three
This article explains how to set up a WCF client to download the data from the server in asynchronou...
Transaction Propagation in WCF
In this article, I will explain how to configure transaction propagation in WCF.

You might also be interested in the following related blog posts


Self-reference hierarchy with Telerik TreeView for Silverlight read more
Announcing Microsoft Ajax Library (Preview 6) and the Microsoft Ajax Minifier read more
Building a class browser with Microsoft Ajax 4.0 Preview 5 read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 7: ADO.NET Data Services Based Data Store read more
Scenarios for WS-Passive and OpenID read more
Introducing Versatile DataSources read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 19: ASP.NET Dynamic Data read more
Convert Web.UI ASP.NET Grid into a data entry spreadsheet read more
Web.UI 2008.2 Grid News: Grouping read more
Web.UI 2008.2 Grid News: Grouping read more
Top
 
 
 

Please login to rate or to leave a comment.

Product Spotlight