Figure 1311

The code for the form is as follows:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles MyBase.Load

TextBox1.Text = 21 TextBox2.Text = 21 End Sub

Private Sub Button1_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button1.Click Dim result As Integer

Dim ws As New CalculatorService.CalculatorClient() ws.Open()

If RadioButton1.Checked = True Then result = ws.Add(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text)) ElseIf RadioButton2.Checked = True Then result = ws.Subtract(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text)) ElseIf RadioButton3.Checked = True Then result = ws.Multiply(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text)) ElseIf RadioButton4.Checked = True Then result = ws.Divide(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text))

End If ws.Close()

Label1.Text = result.ToString() End Sub

End Class

Code snippet from Form1.vb

This is quite similar to the steps taken when working with Web references from the XML Web Services world. First is an instantiation of the proxy class, as shown with the creation of the svc object:

Dim ws As New CalculatorService.CalculatorClient()

Working with the ws object now, the IntelliSense options provide you with the appropriate Add, Subtract, Multiply, and Divide methods. Running this application provides results similar to those presented in Figure 13-12.

In this case, the Add method is invoked from the service when the form's Calculate button is pressed.

Another best practice is to get a tool such as Fiddler2 to track communication with your service. (www.fiddler2.com/ fiddler2/) This free tool enables you to view messages sent across a HTTP/HTTPS.

Note that while this tool will work if you've used Visual Studio to configure your testing to be transported via HTTP, if you've relied on the custom client, you'll find that the requests are instead sent as binary data over TCP and are not available to Fiddler2.

Using a binding to the custom client means the requests and responses are sent over TCP as binary, dramatically decreasing the size of the payload for large messages. This is something that .NET Remoting was used for prior to the release of the WCF framework.

This concludes the short tutorial demonstrating how to build a single service that can support two different endpoints. Visual Studio 2010 can generate one such endpoint, which is based on the same XML and open standards as a traditional Web service. The other you built manually into your command-line application to support the TCP protocol and binary data transfer. Depending on how you map that service to your client, you can consume the service as either an XML-based data transfer or a binary data transfer that can map directly into your .NET Windows Forms application.

0 0

Post a comment