Creating and Consuming .NET Web Services in 5 Easy StepsBy Dimitrios Markatos
This article was written in 2002 and remains one of our most popular posts. If you’re keen to learn more about .NET, you may find this recent article on combining LESS with ASP.NET of great interest.
The Internet has a new player on the scene. It’s been surrounded by a great deal of hype — and even some television commercials! Apparently, this new, "next generation technology" will change the way business is done on the Web. It seems that soon, companies, their applications or software, and any Internet-enabled devices will easily be able to communicate with, and provide services to, one another regardless of platform or language. Sounds revolutionary!
So, what exactly is it that will open these boundless lines of communication? Web Services, that’s what!
Web services give developers the ability to utilize four open Web standards:
- HTTP – Hypertext Transfer Protocol
The standard protocol used over Port 80, which traverses firewalls, and is responsible for requesting and transmitting data over the Internet.
- SOAP – Simple Object Access Protocol
An XML-inherent protocol that encloses a set of rules for data description and process. As a standard, this is the center-piece that complements the other three standards mentioned here.
- XML – Extensible Markup Language
The most common markup language in which all this information is written.
- WSDL – Web Services Description Language
An XML-based method used to identify Web Services and their access at runtime. .NET provides a tool called WSDL.exe, which essentially makes it quite easy to generate an XML Web service as an XML file. This contains all the methods and instructions the Web Service has, and typically uses SOAP as its default.
This article will see you create and consume a data-driven .NET XML Web service in 5 quick and easy steps!
I’ll assume that you have a decent grasp of common .NET data access, Web server controls, such a datagrid, and some object-oriented programming concepts. If not, don’t worry too much. If you complete the examples, and view the results, you should have no difficulty in keeping up and observing the causes and effects of what this tutorial entails.
Prior to .NET, there were other alternatives that could be used to access a Web service, such as Microsoft’s MSXML component, which enabled you to communicate with the given Web Service over HTTP POST. However, this process, while acceptable, is just not .NET.
Ok, let’s begin!
Step 1 – Create the Web Service
First we’ll create the Web service function or method that’ll you’ll call (or “expose”) over the Internet as you would any object-oriented class. The difference is that we’ll have to incorporate and import all the necessary Web Services namespaces, syntax and attributes, as well as our data namespaces, in this case. As this article uses C#, any important differences regarding VB will be shown as well.
So go ahead and copy the code below to a file called suppliers.asmx. Save it to your Inetpub/wwwroot folder, and then run it in your browser using http://localhost/suppliers.asmx. What you’ll see is a list of the Web Service Descriptions, including the Web service name and the exposed method.
By clicking the exposed method link, you’ll be presented with the three main protocols that are available for your use. Just so you know, the file with the .asmx extension is the actual Web Service file that enables the ASP.NET runtime to return all pertinent exposed Web service methods and information.
<%@ WebService Language="C#" Class="GetInfo" %>
[WebService(Description="My Suppliers List Web Service")]
public class GetInfo : WebService
public DataSet ShowSuppliers (string str)
SqlConnection dbConnection = new SqlConnection("server=(local);
SqlDataAdapter objCommand = new SqlDataAdapter("select
ContactName, CompanyName, City, Phone from Suppliers
where Country = '" + str + "' order by ContactName
DataSet DS = new DataSet();
dbConnection = null;
<%@ WebService Language="C#" Class="GetInfo" %> directive sets up the file as a Web Service, gives it a name and specifies the language it uses.