There are two primary steps when working with Web services: creating a Web service and accessing a Web service. We discussed how to create a Web service and test it in the IE test page. Let us now explore how to develop a Windows Form client. In consuming a Web service, the client application has to locate, reference, and invoke the functionality in a Web service. A Web service client can be any type of .NET application, such as a Web or Windows application. Let's create a simple Windows Form client for the Concatenate Web service. The fundamental steps in consuming Web services are as follows :
Generating a ProxyA proxy class acts as an intermediary between the Web service and the client. It transmits the required arguments from the client to the Web service and also returns of the result back to the client. By default, the proxy class uses SOAP to access the Web service method. Generating a proxy class is very easy. You can create it in two ways.
Creating a Proxy Using Wsdl.exeNow let us create a proxy for Concatenate.asmx and explore the proxy class in detail. You have to specify the name of the proxy file to be created and the URL where the service description is available, as follows: Wsdl /out:Concatenate.cs http://localhost/Security /AppendixE/Concatenate/Concatenate.asmx?wsdl The Wsdl.exe tool creates a C# proxy class by default. For other languages, such as VB.NET or JScript.NET, you have to use the optional /l: language flag. CONSTRUCTING THE ASSEMBLYTo make use of the generated proxy class, you have to build an assembly to contain the proxy code. Construct the assembly in the command prompt as follows: csc /r:system.xml.dll /r:system.web.services.dll /out:c:\Concatenate.dll /t:library Concatenate.cs The syntax for the Wsdl tool is as follows: Wsdl /language:language /protocol:protocol /namespace:myNameSpace /out:filename /username:username /password:password /domain:domain <url or path> Figure E-11 shows the Concatenate.dll assembly in IL DASM. [4]
Figure E-11. Viewing the Concatenate.dll assembly in IL DASM.
Creating a Windows Form ClientNow we create a simple Windows Form client using Visual Studio .NET, which consumes the Concatenate Web service. Create a Windows application project and add a reference to the Concatenate.dll assembly and System.Web.Services . Then, you can easily access the Web service by creating an instance of the proxy object in the client code, as follows: private void button1_Click(object sender, System.EventArgs e) { string s1,s2; Concatenation proxy = new Concatenation(); s1=textBox1.Text.ToString(); s2=textBox2.Text.ToString(); textBox3.Text=proxy.Concatenate(s1,s2); } Figure E-12 shows the output of the Windows Form client program. Figure E-12. Output of the Concatenationwinclient program.
CREATING A WEB REFERENCE IN VISUAL STUDIO .NETIn Visual Studio .NET creating a Web reference is straightforward. The only limitation when you use Visual Studio .NET is that you can specify the protocol as SOAP only. But you can specify the protocol as HTTP GET, HTTP POST, or SOAP in the Wsdl.exe utility using the /protocol flag. To add a reference to the Web service, click Project Add Web Reference. Then click the link Web References on Local Web Server or type the required Web service path name in the address. You can view the test page and WSDL file, and then click the Add Reference button. You can view the disco [5] and WSDL files in the Web References node in the Solution Explorer [6] . Then, you can easily access the Web service by creating an instance of the proxy object in the client code, as follows:
private void button1_Click(object sender, System.EventArgs e) { string s1,s2; localhost.Concatenation proxy = new localhost.Concatenation(); s1=textBox1.Text.ToString(); s2=textBox2.Text.ToString(); textBox3.Text=proxy.Concatenate(s1,s2); } |