Chapter: Internet & World Wide Web HOW TO PROGRAM - Rich Internet Application Client Technologies - Microsoft ilverlight and Rich Internet Applications

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Consuming a Web Service

The web service is designed to perform calculations with integers that contain a maximum of 100 digits.

Consuming a Web Service

 

In the next example, we consume a web service from a Silverlight application. The web service is designed to perform calculations with integers that contain a maximum of 100 digits. Most programming languages cannot easily perform calculations using integers this large. The web service provides client applications with methods that take two “huge in-tegers” and determine their sum, their difference, which one is larger or smaller and whether the two numbers are equal. We’ve placed the web service is on our website at test.deitel.com/hugeinteger/hugeinteger.asmx.

 

 

We provide a Visual Basic program that consumes this web service. We will create a Silverlight application using that application’s code, then we’ll add a proxy class to the project that allows the Silverlight application to access the web service. The proxy class (or proxy) is generated from the web service’s WSDL file and enables the client to call web methods over the Internet. The proxy class handles all the details of communicating with the web service. The proxy class is hidden from you by default—you can view it in the Solution Explorer by clicking the Show All Files button. The proxy class’s purpose is to make clients think that they are calling the web methods directly.

 

When you add a web reference to the Silverlight project, Visual Studio will generate the appropriate proxy class. You will then create an instance of the proxy class and use it to call the web service’s methods. First, create a new GOA WinForms VB Application named HugeInteger in Visual Studio 2008, then perform the following steps:

Step 1: Opening the Add Web Reference Dialog

Right click the project name in the Solution Explorer and select Add Web Reference… (Fig. 19.26).

 

Step 2: Locating Web Services on Your Computer

 

Note that for the application to work, it must reside on the same server as the web service, because Sil-verlight 1.1 does not yet allow for cross-domain requests. These steps demonstrate the pro-cess we went through to create the application on our server at test.deitel.com/

examples/iw3htp4/silverlight/HugeInteger/.

 

Step 3: Adding the Web Reference

Add the web reference by clicking the Add Reference button (Fig. 19.27).



Step 4: Viewing the Web Reference in the Solution Explorer

The Solution Explorer (Fig. 19.28) should now contain a Web References folder with a node named after the domain name where the web service is located. In this case, the name is com.deitel.test because we are using a web service from test.deitel.com. When we reference class HugeInteger in the client application, we will do so through the com.deitel.test namespace.




 

1. Consuming the HugeInteger Web Service

 

Now, copy the InitializeComponent function (lines 14–109) and Private WithEvents section (lines 111–119) from HugeIntegerForWindows.Designer.vb in the HugeInte-gerForWindows project to MyForm.vb in the HugeInteger project. Delete the old Initial-izeComponent function and Friend WithEvents line in MyForm.vb. Then, copy lines 6– 117 from HugeIntegerForWindows.vb in the HugeIntegerForWindows project into the MyForm Class located in MyForm.vb in the HugeInteger project. If you try to build the project now, you will notice that the code is trying to access properties not supported by GOA WinForms. In Fig. 19.29, we commented out lines 38–41, 85–86, 95–95, 105–107 and 118–119 to remove statements that access unsupported properties in GOA Win-Forms. If you were running this application from the same server as the web service, you could now build this application and run it by opening TestPage.html. Try the completed application at test.deitel.com/examples/iw3htp4/silverlight/HugeInteger/.

 

1    ' Fig. 19.29: MyForm.vb

 

2    ' Consuming the HugeInteger web service in Silverlight 1.1 Alpha Refresh

 

3    Public Class MyForm

 

4          Inherits System.Windows.Forms.Form

5

6          Public Sub New()

7                 MyBase.New()

 

8                 InitializeComponent()

 

9          End Sub

10

11           'Form overrides dispose to clean up the component list.

 

12           Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

 

If disposing Then

14   If Not (components Is Nothing) Then

15   components.Dispose()

16                            End If

 

17                    End If

 

18                    MyBase.Dispose(disposing)

 

19           End Sub

20

21   Private components As System.ComponentModel.IContainer

 

22

23         Private Sub InitializeComponent()

 

24                Me.lblResult = New System.Windows.Forms.Label

 

25                Me.btnEqual = New System.Windows.Forms.Button

 

26                Me.btnSmaller = New System.Windows.Forms.Button

 

27                Me.btnLarger = New System.Windows.Forms.Button

 

28                Me.btnSubtract = New System.Windows.Forms.Button

 

29                Me.btnAdd = New System.Windows.Forms.Button

 

30                Me.txtSecond = New System.Windows.Forms.TextBox

 

31                Me.txtFirst = New System.Windows.Forms.TextBox

 

32                Me.lblPrompt = New System.Windows.Forms.Label

 

33                Me.SuspendLayout()

34

35                ' lblResult

 

' the following four lines were commented out because they

37               ' accessed properties that are not supported in GOA WinForms

 

38                 ' Me.lblResult.BorderStyle = System.Windows.Forms.BorderStyle.

 

39                      ' FixedSingle()

 

40               ' Me.lblResult.Font = New System.Drawing.Font(

 

41                      ' "Microsoft Sans Serif", 9.75!)

 

42               Me.lblResult.Location = New System.Drawing.Point(13, 122)

 

43               Me.lblResult.Name = "lblResult"

 

44               Me.lblResult.Size = New System.Drawing.Size(714, 37)

 

45               Me.lblResult.TabIndex = 17

46

47               ' btnEqual

 

48               Me.btnEqual.Location = New System.Drawing.Point(562, 91)

 

49               Me.btnEqual.Name = "btnEqual"

 

50               Me.btnEqual.Size = New System.Drawing.Size(85, 23)

 

51               Me.btnEqual.TabIndex = 16

 

52               Me.btnEqual.Text = "Equal"

53

54               ' btnSmaller

 

55               Me.btnSmaller.Location = New System.Drawing.Point(445, 91)

 

56               Me.btnSmaller.Name = "btnSmaller"

 

57               Me.btnSmaller.Size = New System.Drawing.Size(85, 23)

 

58               Me.btnSmaller.TabIndex = 15

 

59               Me.btnSmaller.Text = "Smaller Than"

60

61               ' btnLarger

 

62               Me.btnLarger.Location = New System.Drawing.Point(328, 91)

 

63               Me.btnLarger.Name = "btnLarger"

 

64               Me.btnLarger.Size = New System.Drawing.Size(85, 23)

 

65               Me.btnLarger.TabIndex = 14

 

66               Me.btnLarger.Text = "Larger Than"

67

68               ' btnSubtract

 

69               Me.btnSubtract.Location = New System.Drawing.Point(211, 91)

 

70               Me.btnSubtract.Name = "btnSubtract"

 

71               Me.btnSubtract.Size = New System.Drawing.Size(85, 23)

 

72               Me.btnSubtract.TabIndex = 13

 

73               Me.btnSubtract.Text = "Subtract"

74

75                ' btnAdd

 

76               Me.btnAdd.Location = New System.Drawing.Point(94, 91)

 

77               Me.btnAdd.Name = "btnAdd"

 

78               Me.btnAdd.Size = New System.Drawing.Size(85, 23)

 

79               Me.btnAdd.TabIndex = 12

 

80               Me.btnAdd.Text = "Add"

81

82               ' txtSecond

 

83               ' the following two lines were commented out because they

 

84               ' accessed a property that is not supported in GOA WinForms

 

85               ' Me.txtSecond.Font = New System.Drawing.Font(

 

86                      ' "Microsoft Sans Serif", 9.75!)

 

87               Me.txtSecond.Location = New System.Drawing.Point(13, 63)

 

88               Me.txtSecond.Name = "txtSecond"

 

Me.txtSecond.Size = New System.Drawing.Size(714, 22)

90      Me.txtSecond.TabIndex = 11

 

91

92               ' txtFirst

 

93               ' the following two lines were commented out because they

 

94               ' accessed a property that is not supported in GOA WinForms

 

95               ' Me.txtFirst.Font = New System.Drawing.Font(

 

96                      ' "Microsoft Sans Serif", 9.75!)

 

97               Me.txtFirst.Location = New System.Drawing.Point(13, 35)

 

98               Me.txtFirst.Name = "txtFirst"

 

99               Me.txtFirst.Size = New System.Drawing.Size(714, 22)

100     Me.txtFirst.TabIndex = 10

 

101

102            ' lblPrompt

 

103            ' the following three lines were commented out because they

 

104            ' accessed properties that are not supported in GOA WinForms

 

105            ' Me.lblPrompt.AutoSize = True

 

106            ' Me.lblPrompt.Font = New System.Drawing.Font(

 

107                   ' "Microsoft Sans Serif", 9.75!)

 

108            Me.lblPrompt.Location = New System.Drawing.Point(13, 16)

 

109            Me.lblPrompt.Name = "lblPrompt"

 

110            Me.lblPrompt.Size = New System.Drawing.Size(339, 16)

 

111            Me.lblPrompt.TabIndex = 9

 

112            Me.lblPrompt.Text = "Please enter two positive numbers" & _

 

113                   "up to 100 digits each."

114

115            ' UsingHugeIntegerWebService

 

116            ' the following two lines were commented out because they

 

117            ' accessed properties that are not supported in GOA WinForms

 

118            ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

 

119            ' Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

 

120            Me.ClientSize = New System.Drawing.Size(740, 175)

 

121            Me.Controls.Add(Me.lblResult)

  

115            Me.Controls.Add(Me.btnEqual)

 

116            Me.Controls.Add(Me.btnSmaller)

 

117            Me.Controls.Add(Me.btnLarger)

 

118            Me.Controls.Add(Me.btnSubtract)

 

119            Me.Controls.Add(Me.btnAdd)

 

120            Me.Controls.Add(Me.txtSecond)

 

121            Me.Controls.Add(Me.txtFirst)

 

122            Me.Controls.Add(Me.lblPrompt)

 

123            Me.Name = "UsingHugeIntegerWebService"

 

124            Me.Text = "Using Huge Integer Web Service"

 

125            Me.ResumeLayout(False)

 

126            Me.PerformLayout()

127     End Sub

135

136     Private WithEvents lblResult As System.Windows.Forms.Label

 

137     Private WithEvents btnEqual As System.Windows.Forms.Button

 

138     Private WithEvents btnSmaller As System.Windows.Forms.Button

 

139     Private WithEvents btnLarger As System.Windows.Forms.Button

 

140     Private WithEvents btnSubtract As System.Windows.Forms.Button

 

141     Private WithEvents btnAdd As System.Windows.Forms.Button

 

Private WithEvents txtSecond As System.Windows.Forms.TextBox

143      Private WithEvents txtFirst As System.Windows.Forms.TextBox

 

144      Private WithEvents lblPrompt As System.Windows.Forms.Label

145      

146      Public Shared Sub Main()

 

147             Application.Run(New MyForm)

148      End Sub

149

150      ' declare a reference to Web service

 

151      Private remoteInteger As New com.deitel.test.HugeInteger

152      

153      ' character to trim from strings

 

154      Private zeros As Char() = New Char() {"0"c}

155      

156      ' adds two numbers input by user

 

157      Private Sub btnAdd_Click(ByVal sender As System.Object, _

 

158             ByVal e As System.EventArgs) Handles btnAdd.Click

 

159             ' make sure numbers do not exceed 100 digits and that both

 

160             ' are not 100 digits long, which would result in overflow

 

161             If txtFirst.Text.Length > 100 Or txtSecond.Text.Length > 100 _

 

162                   Or (txtFirst.Text.Length = 100 And _

 

163                         txtSecond.Text.Length = 100) Then

 

164                   MessageBox.Show( _

 

165                         "HugeIntegers must not be more than 100 digits" & _

 

166                         vbCrLf & "Both integers cannot be of length 100: " & _

 

167                         " this causes an overflow", "Error", _

  

156                         MessageBoxButtons.OK, MessageBoxIcon.Information)

157             Else

 

158                   ' perform addition

 

159                   lblResult.Text = remoteInteger.Add( _

 

160                         txtFirst.Text, txtSecond.Text).TrimStart(zeros)

161             End If

 

162      End Sub ' btnAdd_Click

175

176      ' subtracts two numbers input by user

 

177      Private Sub btnSubtract_Click(ByVal sender As System.Object, _

 

178             ByVal e As System.EventArgs) Handles btnSubtract.Click

 

179             ' ensure that HugeIntegers do not exceed 100 digits

 

180             If Not NumbersTooBig(txtFirst.Text, txtSecond.Text) Then

 

181                   ' perform subtraction

182                   Try

 

Dim result As String = remoteInteger.Subtract( _

184                       txtFirst.Text, txtSecond.Text).TrimStart(zeros)     

185                 

186  If result = "" Then 

187        lblResult.Text = "0"

188  Else

189        lblResult.Text = result   

190                         End If

 

191                   Catch exception As Exception

 

192                         ' if WebMethod throws an exception,

 

193                         ' then first argument was smaller than second

 

194                         MessageBox.Show( _

195  "First argument was smaller than the second")

196                   End Try

197             End If

 

198      End Sub ' btnSubtract_Click

199      

200      ' determines whether first number input is larger than the second

 

201      Private Sub btnLarger_Click(ByVal sender As System.Object, _

 

202             ByVal e As System.EventArgs) Handles btnLarger.Click

 

 

200             ' ensure that HugeIntegers do not exceed 100 digits

 

201             If Not NumbersTooBig(txtFirst.Text, txtSecond.Text) Then

 

202                   ' call Web-service method to determine whether

 

203                   ' first integer is larger than the second

 

204                   If remoteInteger.Bigger(txtFirst.Text, txtSecond.Text) Then

 

205                         lblResult.Text = txtFirst.Text.TrimStart(zeros) & _

209             " is larger than " & txtSecond.Text.TrimStart(zeros)

 

210                   Else

 

211                         lblResult.Text = txtFirst.Text.TrimStart(zeros) & _

212             " is not larger than " + txtSecond.Text.TrimStart(zeros)

 

213                   End If

214             End If

 

215      End Sub ' btnLarger_Click

216      

217      ' determines whether first number input is smaller than the second

 

218      Private Sub btnSmaller_Click(ByVal sender As System.Object, _

 

219             ByVal e As System.EventArgs) Handles btnSmaller.Click

 

220             ' make sure HugeIntegers do not exceed 100 digits

 

 

217             If Not NumbersTooBig(txtFirst.Text, txtSecond.Text) Then

 

218                   ' call Web-service method to determine if

 

219                   ' first integer is smaller than second

 

220                   If remoteInteger.Smaller(txtFirst.Text, txtSecond.Text) Then

 

221                         lblResult.Text = txtFirst.Text.TrimStart(zeros) & _

226             " is smaller than " + txtSecond.Text.TrimStart(zeros)

 

227                   Else

 

228                         lblResult.Text = txtFirst.Text.TrimStart(zeros) & _

229             " is not smaller than " & txtSecond.Text.TrimStart(zeros)

 

230                   End If

231             End If

 

232      End Sub ' btnSmaller_Click

233      

234      ' determines whether the two numbers input are equal

 

235      Private Sub btnEqual_Click(ByVal sender As System.Object, _

 

236             ByVal e As System.EventArgs) Handles btnEqual.Click

 

237             ' ensure that HugeIntegers do not exceed 100 digits

 

238             If Not NumbersTooBig(txtFirst.Text, txtSecond.Text) Then

 

239                   ' call Web-service method to determine if integers are equal

 

240                   If remoteInteger.EqualTo(txtFirst.Text, txtSecond.Text) Then

 

241                         lblResult.Text = txtFirst.Text.TrimStart(zeros) & _

242             " is equal to " & txtSecond.Text.TrimStart(zeros)

 

243                   Else

 

244                         lblResult.Text = txtFirst.Text.TrimStart(zeros) & _

245             " is not equal to " & txtSecond.Text.TrimStart(zeros)

 

246                   End If

247             End If

 

End Sub ' btnEqual_Click

250      ' determines whether numbers input by user are too big

 

251      Private Function NumbersTooBig(ByVal first As String, _

 

252             ByVal second As String) As Boolean

 

253             ' display an error message if either number has too many digits

 

254             If (first.Length > 100) Or (second.Length > 100) Then

 

255                   MessageBox.Show("HugeIntegers must be less than 100 digits", _

 

256                         "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)

 

257                   Return True

258             End If

259

260             Return False

 

261      End Function ' NumbersTooBig

262      

263  End Class

 

Fig. 19.29 | Consuming the HugeInteger web service in Silverlight 1.1 Alpha.

 

The code in Fig. 19.29 uses the HugeInteger web service to perform computations with positive integers up to 100 digits long. You are already familiar with converting a Visual Basic Windows Forms application to Silverlight, so we focus our discussion on the web services concepts in this example.

 

Line 151 creates variable remoteInteger and initializes it with a proxy object of type com.deitel.test.HugeInteger. This variable is used in each of the application’s event handlers to call methods of the HugeInteger web service. Lines 171–172, 183–184, 207, 224 and 240 in the various button event handlers invoke methods of the web service. Note that each call is made on the local proxy object, which then communicates with the web service on the client’s behalf.

 

The user inputs two integers, each up to 100 digits long. Clicking a button causes the application to invoke a web method to perform the appropriate task and return the result. Note that client application HugeInteger cannot perform operations using 100-digit numbers directly. Instead the application creates String representations of these numbers and passes them as arguments to web methods that handle such tasks for the client. It then uses the return value of each operation to display an appropriate message.

 

Note that the application eliminates leading zeros in the numbers before displaying them by calling String method TrimStart, which removes all occurrences of characters specified by a Char array (line 154) from the beginning of a String.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.