Building XML Web Services Using C# and ASP.NET

A simple web service

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
{
[WebMethod]
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.

[Note] The file extension for a web service is .asmx. [End Note]

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.

[WebMethod]

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.

You might also like...

Comments

About the author

James Yang Australia

James is a student at Georgia Institute of Technology, majoring in Computer Science. He is an MCSE, MCDBA, MCSA and CCNA.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”