330Microsoft Visual Studio 2010: A Beginner’s Guide

namespace of the example code for this chapter is CustomerConsole, the namespace of the Web service proxy is CustomerConsole.CustomerService. Here’s code that instantiates the proxy:

C#:

var svc = new CustomerServiceClient();

VB:

Dim svc = New CustomerServiceClient

The proxy is named after the service reference, with Client appended to the name. As with any other class, you instantiate the proxy, resulting in a reference to the proxy, named svc. Using the proxy makes your code feel like everything is in the same project, but really the proxy makes a call over HTTP, sending an XML package to the Web service. The Web service translates the XML into a method call, executes the code for the method call, and translates the results back into XML. Meanwhile, the proxy is waiting on the Web service and will receive the XML response, translate that response into a .NET object, and pass the object back to your calling code. If the method returns void instead of a type, then there isn’t any value to return.

With the service reference, you can begin communicating with the Web service. The following example creates a new customer record, calling the InsertCustomer method on the Web service proxy:

C#:

var newCust = new Customer

{

Age = 36,

Birthday = new DateTime(1974, 8, 22), Income = 56000m,

Name = "Venus"

};

var newCustID = svc.InsertCustomer(newCust);

VB:

Dim newCust = New Customer

With newCust

.Age = 36

.Birthday = New DateTime(1974, 8, 22)

.Income = 56000

Page 353
Image 353
Microsoft 9GD00001 manual Microsoft Visual Studio 2010 a Beginner’s Guide