Creating the Proxy

Before you can create a client application to interact with the calculator web service, you must first create a proxy class. Once again, you can do this by hand, but that would be hard work. The folks at Microsoft have provided a tool called wsdl that generates the source code for the proxy based on the information in the WSDL file.

To create the proxy, you enter wsdl at the Windows command-line prompt, followed by the path to the WSDL contract. For example, you might enter:

wsdl http://localhost/webforms/wscalc/service1.asmx?wsdl

The result is the creation of a C# client file named Service1.cs, an excerpt of which appears in Example 16-3. You must add the namespace WSCalc because you’ll need it when you build your client (the tool does not insert it for you).

Example 16-3. Sample client code to access the calculator web service

using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.Web.Services;

namespace WSCalc { [System.Web.Services.WebServiceBindingAttribute( Name="Service1Soap", Namespace="http://www.libertyAssociates.com/webServices/")] public class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol { public Service1( ) { this.Url = "http://localhost/webforms/wscalc/service1.asmx"; } [System.Web.Services.Protocols.SoapDocumentMethodAttribute( "http://www.libertyAssociates.com/webServices/Add", RequestNamespace= "http://www.libertyAssociates.com/webServices/", ResponseNamespace= "http://www.libertyAssociates.com/webServices/", ...

Get Programming C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.