Make your own free website on Tripod.com

Web Services and the .NET Framework
Building a Web Service Client using C# and .NET

Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................Microsoft .NET SDK
.........................................Microsoft Visual Studio.NET Beta 2 or higher

 

Building Web Service Clients using the .NET Framework

Clients communicate with Web Services using standard web protocols, using XML-encoded messages to send and receive messages between themselves. This makes it possible for client applications running on any platform to access web services as long as they use standard web protocols, and understand the XML-encoded messages. There are three protocols that web clients can use to communicate with web services namely, HTTP GET, HTTP POST, and SOAP.

The various steps that are involved in creating a Web Service Client using C# and the .NET Framework are as follows:

  1. Generate a Proxy for the Web Service

  2. Compile the Proxy as a DLL library

  3. Create a Visual C# - Console Application Project

  4. Develop the Client.cs class file

  5. Build the Project Files

1. Generate a Proxy class for the Web Service

The .NET SDK simplifies the process of creating Web Service clients by providing the Web Services Description Language (wsdl.exe) utility. This utility generates proxy source code for an existing Web Service, just as IDL compilers generate DCOM proxies for DCOM components. The only difference between IDL and WSDL is that, WSDL is a language that describes the interface of a software component that is XML-based.

Let us generate source code for the proxies to the actual web service as shown below:

Command Prompt
C:\MyProjects\Cornucopia\WebService\Client>wsdl /l:CS /protocol:SOAP http://localhost/OIDServer/OIDServer.asmx?WSDL
Microsoft (R) Web Services Description Language Utility
[Microsoft (R) .NET Framework, Version 1.0.2914.16]
Copyright (C) Microsoft Corp. 1998-2001. All rights reserved.

Writing file 'C:\MyProjects\Cornucopia\WebService\Client\OIDServer.cs'.

C:\MyProjects\Cornucopia\WebService\Client>

The above command creates a proxy for the OIDServer web service from the WSDL document obtained from the URL  http://localhost/OIDServer/OIDServer.asmx?WSDL. The proxy uses SOAP as its protocol to talk to the web service and is generated as a C# source file which is shown below for your perusal.

OIDServer.cs
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.0.2914.16
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

//
// This source code was auto-generated by wsdl, Version=1.0.2914.16.
//
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.Web.Services;


[System.Web.Services.WebServiceBindingAttribute(Name="OIDServerSoap",
                                                                             Namespace="http://icommware.com")]
public class OIDServer : System.Web.Services.Protocols.SoapHttpClientProtocol {

  [System.Diagnostics.DebuggerStepThroughAttribute()]
  public OIDServer() {
    this.Url = "http://localhost/OIDServer/OIDServer.asmx";
  }

  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://icommware.com/generateOID",
                                                                                                 RequestNamespace="http://icommware.com",
                                                                                                 ResponseNamespace="http://icommware.com",
                                                                                                 Use=System.Web.Services.Description.SoapBindingUse.Literal,
                                                                                                 ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
  public string generateOID() {
    object[] results = this.Invoke("generateOID", new object[0]);
    return ((string)(results[0]));
  }

  [System.Diagnostics.DebuggerStepThroughAttribute()]
  public System.IAsyncResult BegingenerateOID(System.AsyncCallback callback, object asyncState) {
    return this.BeginInvoke("generateOID", new object[0], callback, asyncState);
  }

  [System.Diagnostics.DebuggerStepThroughAttribute()]
  public string EndgenerateOID(System.IAsyncResult asyncResult) {
    object[] results = this.EndInvoke(asyncResult);
    return ((string)(results[0]));
  }
}

The wsdl.exe utility can also take a WSDL file as input instead of a URL pointing to the location where the WSDL can be obtained. This C# proxy source file represents the proxy class for the OIDServer web service that clients can compile against. If you examine the above class, you will notice that it contains an OIDServer proxy class that derives from the System.Web.Services.Protocols.SoapHttpClientProtocol class. If you use the /protocol:HttpGet or /protocol:HttpPost parameters, the OIDServer derives from either the System.Web.Services.Protocols.HttpGetClientProtocol class or the System.Web.Services.Protocols.HttpPostClientProtocol class.

2. Compile the Proxy class as a DLL Library

We can compile the C# source file into a dynamic link library (DLL) and then add a reference to this DLL to any project you want to create. Compile the proxy class as a DLL as shown below: 

Command Prompt
C:\MyProjects\Cornucopia\WebService\Client>csc /t:library /r:System.Web.Services.dll /r:System.Xml.dll OIDServer.cs
Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.


C:\MyProjects\Cornucopia\WebService\Client>

3. Create a Visual C# - ASP.NET Web Service project

Create a new Visual C# ASP.NET Web Service project. Create a Reference to the OIDServer.dll library.

 

4. Develop the Client.cs class file

On Line 40, we create an instance of the proxy to the web service OIDServer. Line 43 invokes the generateOID() web method to get a string as the result.

Client.cs
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
//////////////////////////////////////////////////////
/// The following example illustrates a Client to a
/// WebService developed using C# and the .NET Framework.
///
/// author: Gopalan Suresh Raj
/// Copyright (c), 2002. All Rights Reserved.
/// URL: http://gsraj.tripod.com/
/// email: gopalan@gmx.net
///
/// <generate>
/// wsdl /l:CS /protocol:SOAP http://localhost/OIDServer/OIDServer.asmx?WSDL
/// wsdl /l:CS /protocol:HttpGet http://localhost/OIDServer/OIDServer.asmx?WSDL
/// wsdl /l:CS /protocol:HttpPost http://localhost/OIDServer/OIDServer.asmx?WSDL
/// </generate>
/// <compile>
/// csc /t:library /r:System.Web.Services.dll /r:System.Xml.dll OIDServer.cs
/// </compile>
/// <compile>
/// csc Client.cs /r:OIDServer.dll
/// </compile>
//////////////////////////////////////////////////////
using System;

namespace TestOIDServer {
  /// <summary>
  /// Summary description for Client.
  /// </summary>
  public class Client {
    /// <summary>
    /// Default No argument constructor
    /// </summary>
    public Client() {
    }

    /// <summary>
    /// Entry Point to this Application
    /// </summary>
    public static void Main () {
      // Create a proxy
      OIDServer server = new OIDServer();

      // Invoke generateOID() over SOAP and get the new OID
      string oid = server.generateOID ();

      // Print out the value
      Console.WriteLine ("The new OID is :"+oid);
    }
  }
}

5. Build the Project Files

Build the files that make up the project.

------ Rebuild All started: Project: Client, Configuration: Debug .NET ------

Preparing resources...
Updating references...
Performing main compilation...

Build complete -- 0 errors, 0 warnings
Building satellite assemblies...



---------------------- Done ----------------------

Rebuild All: 1 succeeded, 0 failed, 0 skipped

6. Run the Client

Command Prompt
C:\MyProjects\Cornucopia\WebService\Client>Client
The new OID is :16995955-e0ca-43b3-8279-edc14ca8dbca

C:\MyProjects\Cornucopia\WebService\Client>

 

 

Building a complete Web Service component using C# and .NET
Building a Web Service Client using C# and .NET
Accessing a .NET Web Service using Apache/SOAP and Java

 

Download the entire source code as a zip file.

 

click here to go to
My Advanced C#/.NET Tutorial Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He has co-authored a number of books including "Professional JMS", "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (http://gsraj.tripod.com/) or mail him at gopalan@gmx.net.

 


Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since January 6, 2002.

Last Updated : Jan 06, '02

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2002, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.