Consuming the Web Service from .NET


You could test the functionality of the Dataset component of the Web service by using the simple Web service test utility that launches when you run your application, but the sample files include a test application that shows how to call all the functions of the Web service. The main interface for the test application is shown in Figure 14-5.

click to expand
Figure 14-5: The .NET test application for the free/busy Web service

One thing the test application shows is how to add a reference to a Web service in your Visual Studio .NET applications. To add a reference to any Web service, you choose the Add Web Reference command from the Project menu. You will see the interface shown in Figure 14-6, which allows you to browse Web sites for Web services or query UDDI to find a Web service.

click to expand
Figure 14-6: The Add Web Reference interface

When you add your Web reference, you can browse the contract information or documentation for the Web service before adding a reference. Browsing the documentation for our free/busy service shows the default page you saw earlier when we tested our Web service, as shown in Figure 14-7.

click to expand
Figure 14-7: Browsing the documentation for the free/busy Web service

Once the reference is added, you will see the Web service as part of the Web References section in Solution Explorer in Visual Studio .NET. You can now initialize a variable as an instance of the Web service and use the IntelliSense features of Visual Studio .NET to have autocompletion and function information available for the functions of the Web service.

Calling the Web Service

The following code from the consumer application calls the Web service:

 Private Sub frmMain_Load(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles MyBase.Load          'Fill the date with the current date     txtDate.Text = Month(Now) & "/" & DatePart(DateInterval.Day, Now) & _                    "/" & Year(Now) End Sub      Private Sub cmdGetFB_Click(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles cmdGetFB.Click          On Error Resume Next     'Validate some values     If txtSMTPAddress.Text = "" Then         MsgBox("You must enter a valid SMTP Address!", _                MsgBoxStyle.Critical + MsgBoxStyle.OKOnly)         Exit Sub     End If          If (txtDate.Text = "") Or Not (IsDate(CDate(txtDate.Text))) Then         MsgBox("You must enter a valid Date!", _                MsgBoxStyle.Critical + MsgBoxStyle.OKOnly)         Exit Sub     End If          'Figure out which version of the web service we need to call     Dim oWS As New localhost.Service1()     If RadioRaw.Checked = True Then         'Need to just get the raw data         'See if we need to call asynchronously         If checkAsync.Checked = True Then             'Async             Dim olocalhost As localhost.Service1 = _                 New localhost.Service1()             Dim oCB As AsyncCallback             oCB = New AsyncCallback(AddressOf frmMain.RawCallBack)             Dim ar As IAsyncResult = oWS.BeginGetFreeBusyRaw(_                 txtSMTPAddress.Text, txtDate.Text, oCB, olocalhost)         Else             'Sync             DataGrid1.Visible = False             txtResponse.Visible = True             txtResponse.Text = oWS.GetFreeBusyRaw(txtSMTPAddress.Text, _                                                   txtDate.Text)         End If     ElseIf RadioFormatted.Checked = True Then         'Get Formatted         DataGrid1.Visible = False         txtResponse.Visible = True         txtResponse.Text = oWS.GetFreeBusyXml(txtSMTPAddress.Text, _                                               txtDate.Text)     ElseIf RadioDataSet.Checked = True Then         'Get Dataset         Dim oDataSet1 As New DataSet()         oDataSet1 = oWS.GetFreeBusyDataSet(txtSMTPAddress.Text, _                                            txtDate.Text)         DataGrid1.SetDataBinding(oDataSet1, "")         txtResponse.Visible = False         DataGrid1.Visible = True     End If End Sub      Public Shared Sub RawCallBack(ByVal ar As IAsyncResult)     Dim oFinishService As localhost.Service1 = ar.AsyncState     Dim results As String          ' Get the completed results.     results = oFinishService.EndGetFreeBusyRaw(ar)     MsgBox("The results from the async call were: " & results) End Sub      Private Sub RadioFormatted_CheckedChanged(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles RadioFormatted.CheckedChanged          If RadioFormatted.Checked = True Then         checkAsync.Enabled = False     End If End Sub      Private Sub RadioRaw_CheckedChanged(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles RadioRaw.CheckedChanged          If RadioRaw.Checked = True Then         checkAsync.Enabled = True     End If End Sub      Private Sub RadioDataSet_CheckedChanged(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles RadioDataSet.CheckedChanged          If RadioDataSet.Checked = True Then         checkAsync.Enabled = False     End If End Sub 

The first thing you will notice in the cmdGetFB_Click subroutine is that the variable oWS is initialized to our Web service. When you call the functions of a Web service, you have two options for calling the Web service. You can either call the function synchronously and your application will wait for the Web service response before continuing, or you can call the Web service asynchronously.

There is no special code in our Web service that makes the calls asynchronous or synchronous. Instead, when Visual Studio .NET generates the WSDL file for our Web service, it generates the standard synchronous and asynchronous versions of the methods in our class.

The caller does have to write some special code, however, to be able to call the Web service asynchronously. You will notice that the first thing the asynchronous version does is create a new AsyncCallback object. When you call asynchronous methods in .NET, you must pass a delegate that .NET can call when the asynchronous method completes. To pass this function, when the constructor for the AsyncCallback object is called, the code passes the address of the callback function called RawCallBack . It then calls the asynchronous version of the method, which normally starts with begin , as in begingetfreebusyraw .

When the Web service completes and calls the RawCallBack function, it passes an IAsyncResult object. The IAsyncResult object contains the status of an asynchronous operation. This object has a property called AsyncState , which contains the results of the asynchronous operation. The object also has another property called IsCompleted , which we can check to see if the asynchronous operation completed. Because we are using a callback function, we'll assume that the operation completed when we get the callback.

The next step is to get the completed results. To do this, we call the EndGetFreeBusyRaw method on our Web service and pass it our IAsyncResult object. The code simply writes out the results of the Web service. By using an asynchronous call, our code could have continued processing some other code while making the call to the Web service.

The next part of the code to note is the part that works with the DataGrid object to display our DataSet returned from the Web service. A DataGrid object is a new control similar to the FlexGrid in Visual Studio 6.0. The DataGrid allows you to display data in a scrollable grid. What makes the DataGrid useful is that it works directly with a DataSet . Using the SetDataBinding method, you can pass a DataSet as the table for the DataGrid to bind to. The DataGrid then inherits the column names and schema from the passed DataSet and displays the information from the DataSet , as shown in Figure 14-8.

click to expand
Figure 14-8: Displaying a Dataset in a Datagrid control



Programming Microsoft Outlook and Microsoft Exchange 2003
Programming MicrosoftВ® OutlookВ® and Microsoft Exchange 2003, Third Edition (Pro-Developer)
ISBN: 0735614644
EAN: 2147483647
Year: 2003
Pages: 227
Authors: Thomas Rizzo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net