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.
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
You might also like...
About the author
James Yang
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.
ASP.NET books
-
Expert WCF 4: SOA 2.0 with Windows Communication Foundation 4
Windows Communication Foundation has become an integral part of many .NET based solutions, enabling highly customizable messaging across distributed environments. In Expert WCF 4, you will cover scenarios that include designing, implementing, consumi...
ASP.NET forum discussion
-
edmonton female escort services near me
by canadapleasure (0 replies)
-
USB Drive Activity Logger!
by coque0912 (7 replies)
-
Bagaimana memenangkan $ 1,54 miliar dalam Mega Jutaan
by gametogelan (0 replies)
-
Software development company GroupBWT
by alexthunders01 (0 replies)
-
The requested URL was not found on this server
by haulexgem (0 replies)
ASP.NET podcasts
-
Visual Studio Talk Show (en français): Louis-Philippe Pinsonneault
Published 7 years ago, running time 1h12m
20 mars 2013 (Ãmission #0157) ::.Louis-Philippe Pinsonneault: Le "App store" de Windows 8Nous discutons avec Louis-Philippe Pinsonneault du "App store" de Windows 8. Nous essaieront de couvrir tout ce quâil y a à savoir sur le "App store" : les types de licences, les modèles de reven.
ASP.NET jobs
-
Web Systems Developer
Red Gate Software in Cambridge, United Kingdom
45,000 -
Web Application Developer
Red Gate Software in Cambridge, United Kingdom
£35,000-45,000 GBP per year -
Senior Software Engineer
@ One Limited in London, United Kingdom
-
Jr. .NET Developer
T-Symmetry in Lakewood, United States
Comments