Accessing a Web Service


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 :

  • Locate the Web service by using the Add Web Reference dialog box (if you are using Visual Studio .NET).

  • Generate a proxy class for the Web service either by using the WSDL.exe tool or by adding a Web reference to your project.

  • Reference the proxy class in the client code by including its namespace.

  • Create an instance of the Web service proxy class and invoke the Web service methods using that proxy.

Generating a Proxy

A 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.

  • Using the Wsdl.exe command-line tool. [3]

    [3] The Wsdl.exe tool is in the .NET framework directory, such as C:/Program Files/Microsoft.NET/FrameworkSDK/Bin.

  • Using Visual Studio .NET.

Creating a Proxy Using Wsdl.exe

Now 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 ASSEMBLY

To 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]

[4] The Microsoft .NET Framework SDK offers a tool called MSIL Disassembler (ILDasm.exe) that allows you to load any Microsoft .NET assembly (EXE or DLL) and investigate its contents, including the associated manifest, type metadata, and IL instruction set.

Figure E-11. Viewing the Concatenate.dll assembly in IL DASM.

graphics/efig11.gif

Creating a Windows Form Client

Now 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.

graphics/efig12.gif

CREATING A WEB REFERENCE IN VISUAL STUDIO .NET

In 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:

[5] The DISCO specification defines an algorithm for locating service descriptions. The disco file is an XML document that contains links to Web services, or it can offer a dynamic list of Web services in a specified path.

[6] If the Web service is changed, then you have to update the proxy class by right-clicking on the server name (in our case, localhost) and click Update Web Reference.

 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);     } 


.NET Security and Cryptography
.NET Security and Cryptography
ISBN: 013100851X
EAN: 2147483647
Year: 2003
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net