Published: 11 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

We described the WCF service and the WCF client in Part one and two of this article as the infrastructure for the fast download of data from the VFP server. We can present the received data in VFP form or in its OLE object (COM component) in our case the .NET user control decorated with the COM visible commands, COM interface and other registration elements. There is significant difference of at least 19% if we download the retrieved data as a DBF file. We preset the measurement in two tables.

The user control

Create a new project in Visual Studio, select the Interop User Control Library template, and name it NetComGrid.

Listing 8: Net DataGrid control with a COM visible interface

Copy the program in Listing 8. Paste it in the class created by the project. Rename the class to ComGrid. (For a detailed description on how to create the Active-X control see for example [1]. Some notes:

  • Don’t forget to mark the assembly as COM visible (See [1])!
  • In order for our service to be registered and created we add the frame:
  • The GUID created in VS - Tools – Create GUID help us to solve many problems with the registration of the class (COM object). We create two GUIDs:

After building the project, using the object browser we find the object under the name NetComGrid.GridCom.

  • The interface ComEvents allow us to notify the programs (subscribers) of the events fired in this class (The subscribers receive the messages throw this interface when the events based on the delegate IsCompleted and ReceivedData are fired).
  • The two functions framed with [ComRegisterFunction()] register and unregistered the COM class and make it visible as an Active-X control (Visible in VFP – Tools – Options – OleControls). You register the NetComGrid.GridCom control on your development machine when you build the project. On the deployment computer you register the control when you install the project. In the download solution is the project (NetComGrid) and the setup project (NetComGridIn). (How to create and use this project see [4]).

The VFP client

With the VFP form [Figure 1] we retrive the data from the server using five methods:

  • Download a file, write it on the disk and bind it to the VFP datagrid (button File).
  • Download the data directly from the server using the net OleDb driver (without the WCF infrastructure). The received data we bind to the windows datagrid (button Local).
  • Retrieve the data as a DatsSet (button DataSet).
  • Retrieve the data as a DataTable (button DataTable).
  • Retrieve the data as a strong typed Collection (button Collection).

The VFP form has also two other buttons for testing purposes: Test and ComGridRefresh.

Listing 9: The button File click method

Figure 1: The VFP form

The VFP form

The buttons File click method is in Listing 9. Let us first comments the events chain in this method.With the Eventhandler we connect the events of the grid (thisform.olecontrol1.oBJECT) and program (olepublic class) mygridevents. The command BINDEVENT binds the mygridevents.ComEvents_IsCompleted method to the forms onreceive method.

Listing 10: The sourceobjec.prg

We create the sourceobject (Listing 10) with four properties and we pass it to the windows grid (oleobject) method StoreFileComAsyn. This object will return from the ComWcfVfpClient. We will catch it in the form onreceive method (Listing 11)(as the source parameter).

Listing 11: The form onreceive method

The form onreceive method has the same signature as the mygridevents. ComEvents_IsCompleted method. In its parameter (source) it catches the sourceobject sent by the button file click event. It also accepts the name of the dbf file written to the disk (the parameter respStr). It changes the grid record source property to a new file name received from the WCF service.

The data retrieve cycle

When we request the data from the VFP database using the function storefile we follow this path:

The VFP client method calls the ComGrid function StoreFileAsyn and pass it the parameter storefile (the name of the VFP store procedure) and the object sourceobject.

The ComGrid method wcfClient.StoreFileComAsyn(sender, method, OrderIdFrom, orderIdTo, filePath) fires the event StoreFileComAsyn on the wcfClient object. The command wcfClient.ReceivedData += new ComWfcVfpClient.WcfVfpClient.GridEventHandler(this.CollGrid) instructs the client to fire the event this.CollGrid on receiving the results from the service.

Next, we call the method StoreFileCom on a new thread. It will call the method StoreFileCom on the service (server) and will wait for the response from the service. According to the contract, it accepts the error from the server (retError parameter marked as out) and the byte array:

It fills the Received buffer with the dbf files bytes and writes the bytes to the disk dbf file. The method passes the file name to the VFP client (see eventArgs.ReceivedString).

We create the object eventArgs = new ComEventArgs() and set its ReceiveString property value to the name of the dbf file. At last we fire the event this.ReceivedData(this, eventArgs). Using the ReceivedData delegate, we pass the data in the eventArgs object to the NetComGrid.GridCom controls method CollGrid. It fires the event:

The delegate IsCompleted informs the VFP client about two things:

  • The sender (from the VFP client) ea.ReceivedState,
  • The received file name and type (F – file).

We bind the OleControl to the myGridevents.prg (see the EVENTHANDLER), so we connect the NetComGrid interface to the methods in this program.

We bind the event ComEvents_IsCompleted to the form method (event) onreceive:

The onreceive method binds the received dbf file to the datagrid.

The mesurement of the WCF performance

Retrieving the data with oledb driver directly from the server computer (VFP database) is very slow, especially on larger data set it can exceed 1 minute (try the button Local on the VFP form). It is not included in the measurement. We also exclude the two test buttons. We made 10 measurements for the remaining five buttons. We present the results in Table 1 and Table 2.

Table 1: Retrieving time in seconds for the records set of 250 records

 

dbf

Dataset

Collection

DataTable

 

0,53

0,93

0,75

0,75

 

0,17

0,92

0,62

1,56

 

0,64

0,71

1,12

0,51

 

0,57

1,11

0,78

1,01

 

0,17

0,25

0,91

0,42

 

0,64

1,03

0,45

0,79

 

0,93

0,89

0,13

0,98

 

0,28

0,23

0,73

0,56

 

0,87

0,53

0,75

0,54

 

1,04

0,54

0,64

0,34

average

0,58

0,71

0,69

0,75

%

100

122

119

129

min

0,17

0,23

0,13

0,34

The average retrieve (and download) time of the records set of 250 records in a dbf file is 0,58 second (Table 1), 19% slower is the retrieve time of the same records set using the OrderCollection object, 22% slower is downloading this data in the form of the dataset, and 29% is the download with the data table. There is significant difference of at least 19% if we download the data as a DBF file.

Table 2: Retrieving time in seconds for the records set of 2.000 records

 

dbf

Dataset

Collection

DataTable

 

1,29

2,7

2,29

2,31

 

2,12

1,87

1,79

2,57

 

1,75

2,5

1,57

2,57

 

1,78

2,51

2,37

2,81

 

1,54

2,31

1,95

2,85

 

1,81

2,67

1,81

2,87

 

1,91

2,29

1,85

2,81

 

1,18

2,17

2,35

2,75

 

1,89

1,96

2,41

2,64

 

1,34

2,2

2,28

2,42

average

1,66

2,32

2,07

2,66

%

100

140

125

160

min

1,18

1,87

1,57

2,31

%min/average

71

81

76

87

What is the difference in retrieving the records set of 2.000 records? We download the data with the dbf file in average 1,66 seconds (Table 2), that is 25% faster then downloading a collection, 40% slower is the download of data in the form of the dataset and 66% slower using the data table. We suppose the greater part of the retrieval times is due to serialization and transport through the wire.

The WCF infrastructure and the data retrieve

The WCF service is the infrastructure component, so you rarely change it. Chaging and testing it means a lot of work!. Theoretically, you can change the binding, hosting etc. In practice, this is not a trivial process, so do not do this.

Conclusion

In certain cases, downloading data from the VFP database is time consuming. Using the WCF technology, we can improve the retrieve time of almost two times. The benefit of using it is also that we can do this in parallel with other processes.

Apart from this significant time reduction we can optimize the process also inside the WCF infrastructure. The retrieving time of the data with the WCF services depends also from the type of transported data (its serialization etc.). The small samples of experiments we present in this article shows:

  • If we download the data and present it in VFP controls, it is faster, if we download the selected data in the form of the dbf file.
  • If we present the data in the windows ActiveX control it is less work, if we download the data as the Collection object.

If the access to the data is public, it is better if we publish the data as the dataset. In this case the retrieve time costs rise for more than 60% compared with the private dbf retrieve.

There is place for other optimization of the WCF infrastructure. The minimal retrieval time is 1.18 (DBF) and 1.57 (Collection) (Table 2), that is, theoretically we can reduce the retrieval time for 29% and 24% respectively.

We work with a DBF of 400 MB and the corresponding index file of 50MB. Changing these sizes, we obtain different results. It depends also from the used hardware, number of concurrent users etc. However, except for very small record set, there is a big time gap between the direct VFP retrieve and using the WCF service.

References

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

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


WCF 4.0 Features: Part 1
In this article we will see in detail three exciting features of WCF 4.0
Composing WCF applications
This article is taken from the book Dependency Injection in .NET. The author discusses WCF’s extensi...
Create your first RESTful service with WCF
Vishal Nayan explains how to create a RESTful service with WCF.
Calling WCF Duplex service In Silverlight
Vishal Nayan shows how to create a duplex WCF service at server side and let Silverlight access it a...
WCF as a Web Role and Worker Role in Windows Azure - Part 1
In this article we are going to see how WCF plays a major role in application development on Windows...

You might also be interested in the following related blog posts


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
Silverlight Release History : Q2 2009 (version 2009.2.701) read more
WPF Release History : Q2 2009 (version 2009.2.701) read more
Better XAML By Farr: WPF Line of Business Seminar read more
WPF / Silverlight: RadChart and BindableLinq read more
CodeDigest.Com Article,Codes,FAQs - April,2009 read more
Nevron .NET Vision 2009 Vol. 1 is now available for download read more
.NET RIA Services MIX '09 Talk - Slides + Code read more
What is .NET RIA Services? read more
Top
 
 
 

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.