Library tutorials & articles

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.

Comments

  1. 21 Mar 2005 at 21:56
    I got a problem where even after I changed the name, the tempuri persisted, and just figured out that you have to get rid of cached images from:

    C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET FilesNET

    Just wanted to mention this in case someone look for an answer for this.
  2. 22 Jan 2004 at 05:25


    Hi....


    You have to  write the query to check the password. It will be better  if you  use StoredProcedure ..


    Sample of the  stored procedure
    CREATE PROCEDURE _checkpass
    (
    @password varchar(10)
    )


    As
    select password from table where password=@password
    if @@rowcount<1
    select @status=0
    else
    select@status=1
    Go


    Once you write the above stored pro with sql server


    user the following code... in your  application


    myCommand = new SqlCommand("_checkpass", myConnection);
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar, 50));
           myCommand.Parameters["@password"].Value =txtpass.Value;


    myCommand.Connection.Open();


           try
           {
           myCommand.ExecuteNonQuery();


    if((int)status.Value==0)
    {
    Go Ahead........
    }
    else
    {
    Stopped Functioning
    }





  3. 09 Jan 2003 at 20:58
    Posted question without "fully" reading article!!
  4. 09 Jan 2003 at 20:54

    silly question, but as I am new to SQL and C#, how would I be able to check an existing password, that was previously added to your database table and check the result to athenticate the user?

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Building XML Web Services Using C# and ASP.NET .

Leave a comment

Sign in or Join us (it's free).

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

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

We'd love to hear what you think! Submit ideas or give us feedback