Library tutorials & articles
Building XML Web Services Using C# and ASP.NET
- Introduction
- What is a web service?
- A simple web service
- Other web service features
- Real world application
- Conclusion
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:
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:
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:
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:
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.
Related articles
Related discussion
-
Create a Site Search Engine in ASP.NET
by Soundguy53 (64 replies)
-
Read HSQLDB data into a webpage
by joe90 (3 replies)
-
Chart insertation in a windows form...
by pdhanik (1 replies)
-
Writing Plugin-Based Applications
by haneen (12 replies)
-
ASP .NET Web Service Error Message ,"Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'."
by salil15august (1 replies)
Related podcasts
-
Writing FaceBook Applications with .NET - Interview with Mel Sampat, author of Outsync
In this episode, Scott talks with Mel Sampat, a Program Manager at Microsoft who's written OutSync, an application that syncs faces between Outlook, Facebook, and indirectly Windows SmartPhones. They chat about what it takes to write your own FaceBook application using ASP.NET or WinForms.
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.
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.
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
}
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?
This thread is for discussions of Building XML Web Services Using C# and ASP.NET .