Home arrow ASP.NET arrow Page 3 - Building XML Web Services Using C# and ASP.NET

Building XML Web Services Using C# and ASP.NET

The term "web service" has been used quite frequently lately: you hear people saying how good web services are and how they will dominate the future of software development, but what exactly are web services and how can we create them? In this article James shows us how to build two web services and also unravels all of the lingo surrounding web services.

Author Info:
By: James Yang
Rating: 5 stars5 stars5 stars5 stars5 stars / 132
March 29, 2002
  1. · Building XML Web Services Using C# and ASP.NET
  2. · What is a web service?
  3. · A simple web service
  4. · Other web service features
  5. · Real world application
  6. · Conclusion

print this article

Building XML Web Services Using C# and ASP.NET - A simple web service
(Page 3 of 6 )

In this section we'll build a simple web service to demonstrate the fundamentals of web service building. Our web service will contain just one method called HelloWorldMethod. HelloWorldMethod returns a string, "Hello World". We'll add modifications to this web service as we learn more about the available features of web services.

Before building a web service, a virtual directory or web application must be created using IIS. You can do this by loading the IIS snap-in with MMC (start -> run -> "mmc" -> console -> open -> c:\winnt\system32\inetsrv\iis.mmc), right clicking on your web site and choosing new -> virtual directory. Call the virtual directory "HelloWorld".

The following code shows the most basic web service:

<%@ WebService Language="C#" class="HelloWorld"%>

using System.Web.Services;

public class HelloWorld : WebService



public string HelloWorldMethod()


return "Hello World";



Copy the code above into your favourite text editor and save the file into the virtual directory we created above as HelloWorld.asmx.

The file extension for a web service is .asmx.

You can now access our HelloWorld web service in your browser by visiting http://localhost/HelloWorld/HelloWorld.asmx.

You should see a page that looks like this:

Accessing our web service

This web page is automatically built by IIS using the WSDL of our web service. You can change how this page looks but that's out of the scope for this article. You can view the raw WSDL by clicking on the link to "Service Description", which will forward you to the WSDL for our web service, which looks something like this:

The WSDL for our hello world web service

As you can see, the WSDL shows where you can find the web service (URI) and other useful information including the methods that we have created in our web service.

Now that we've built the web service, we need to be able to use it. On the main web service page, you can see the method that we've created. If you click on the method, it will forward you to this page:

The web method page

If our method actually took a parameter, we would be given a text box for us to enter its value into. However, because our method doesn't take any parameters only the invoke button is provided, which will run the method and display the returned output as XML. This page is especially useful for testing purposes during the development and also to allow consumers to check if the web service is what they expected it to be.

When you click on the invoke button, you'll get the following result:

The returned XML

If you examine the XML then you will see that "Hello World" was returned between a <string> tag. As you've probably guessed by now, the tags name specifies the data type of the returned value. Another thing to note is "http://tempuri.org/". This the default namespace assigned to our web service, and unless we change the attribute using the web service attribute, it will display this URI as the namespace of our future web services as well.

Let's now look at the code that made our web service:

<%@ WebService Language="C#" class="HelloWorld"%>

Like any other ASP.NET page, web services also support directives. This just tells the compiler that the C# language is used and that the HelloWorld class contains methods and properties which should be exposed.

public class HelloWorld : WebService

This line is very important. It tells the .NET compiler that our HelloWorld class inherits from the WebService base class, which contains everything that's required to make our class a web service.


This line is written just before the method, and it's a C# attribute that indicates to IIS and the .NET compiler that the following method is to be exposed and web callable. The rest of the code is simple C#, so you shouldn't have any trouble with it.
blog comments powered by Disqus

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials