Web
Services and the .NET Framework
Building a Web Service
Client using C# and .NET
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:
Generate a Proxy for the Web Service
Compile the Proxy as a DLL library
Create a Visual C# - Console Application Project
Develop the Client.cs class file
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 | ||
|
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
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 | ||
|
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 (https://gsraj.tripod.com/) or mail him at gopalan@gmx.net. |
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 |
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. |