Connecting to the web service

To connect to a web service, you must first add a web reference to your project. You've done this earlier in Chapter 6 when you had to connect to a WCF service to synchronize data with a remote server. Right-click on your project, and click on the Add Web Reference menu item.

Browse to the location of your web service's .asmx file and click the Go button to load it. Once the web service is found, name the web reference DashboardDataservice and click the Add Reference button to add this reference to your project.

Now that you have a reference to your web service, let's see how you can call this web service asynchronously from your application.

An asynchronous web service call is a non-blocking call that returns immediately after you execute the request. You specify the address of a call-back function that is automatically invoked when the request completes. Asynchronous web services are commonly used to improve UI responsiveness — the user is free to manipulate the UI while it waits to load data from a web service.

In the code behind the Dashboard form, declare and instantiate a new instance of this web reference:

private DashboardDataService.DataService _service = new DashboardDataService.DataService();

In the load event of the form, call the RefreshAllcharts method:

public void Dashboard_Load(object sender, System.EventArgs e) {

RefreshAllcharts();

The BeginGetchartData method will initiate the asynchronous web service call for the GetchartData web method. You will need to pass in the two arguments for this web method, as well as the address of the call back function (as an AsyncCallback object). You can pass in null for the last parameter of this function.

private void RefreshAllCharts() {

_service.BeginGetChartData(DateTime.Now.Year, DateTime.Now.Month, new

AsyncCallback(BeginGetChartDataCallback), null);

Asynchronous and synchronous web service calls

An asynchronous web service method call has a Begin prefix. To make synchronous (blocking) web service calls, simply use the original web method name itself. Example :_service. GetchartData (DateTime.Now.Year, DateTime.Now.Month)

After the GetchartData web method has completed, your callback function will be invoked. You can declare the callback function shown as follows. You must call the EndGetchartData method to retrieve the return value from the web method.

private void BeginGetchartDatacallback(IAsyncResult Result) {

String _data;

_data = _service.EndGetchartData(Result); MessageBox.Show("Data retrieved : " + _data);

With these few lines of code, you can try connecting to the web service now by running your smart client! Build and deploy this project to the Windows Mobile 6 emulator.

Ensuring your emulator environment is set up correctly

You must first ensure that your emulator is able to connect to the IIS server in your development environment. For this to happen, an ActiveSync session must first be set up between your development machine and the emulator. Please refer to the Connecting the Windows Mobile emulator to ActiveSync section in Chapter 2 for more details on this. You can test if the emulator can reach the IIS server by navigating to your web service from IE Mobile.

After your application loads, you should be able to see the data retrieved from the web service shown in your emulator.

Dashboard Home ^ SalesForceDashboard

Data retrieved : <ChartData> < Roadshows alesxDayl

Value="26159" /xDay2 Value="16859" /xDay3 Value="10540" /xDay4 Value="41531" /xDay5 Value="34334" /xDay6 Value="32972" /xDay7 Value="2789" /xDayS Value="5518" /xDay9 Value="32667" /x Day 10 Value="29585" /x Day 11 Value="610" /xDayl2 Value="11578" /x Day 13 Value="28414" /x Day 14 Value="13059" /x Day 15 Value="539" /><Dayl6

UilLlI

Was this article helpful?

0 0

Post a comment