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" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
[WebService(Description="My Suppliers List Web Service")]
public class GetInfo : WebService
{
[WebMethod(BufferResponse=true)]
public DataSet ShowSuppliers (string str)
{
SqlConnection dbConnection = new SqlConnection("server=(local);
uid=sa;pwd=;database=Northwind;");
SqlDataAdapter objCommand = new SqlDataAdapter("select
ContactName, CompanyName, City, Phone from Suppliers
where Country = '" + str + "' order by ContactName
asc", dbConnection);
DataSet DS = new DataSet();
objCommand.Fill(DS);
return DS;
dbConnection.Close();
dbConnection = null;
}
}
The <%@ WebService Language="C#" Class="GetInfo" %>
directive sets up the file as a Web Service, gives it a name and specifies the language it uses.
Frequently Asked Questions (FAQs) about .NET Web Services
What are the key differences between .NET web services and RESTful web services?
.NET web services and RESTful web services are both used to build web applications, but they have some key differences. .NET web services, also known as ASMX services, use SOAP (Simple Object Access Protocol) for communication, which is a protocol based on XML for exchanging structured information in web services. On the other hand, RESTful web services use HTTP methods like GET, POST, PUT, DELETE for communication. RESTful services are stateless and cacheable, while .NET web services maintain the state of the application.
How can I consume a .NET web service in a client application?
Consuming a .NET web service in a client application involves several steps. First, you need to add a web reference to the service in your project. This can be done by right-clicking on the project in Solution Explorer, selecting “Add Service Reference”, and then entering the URL of the web service. Visual Studio will then generate a proxy class that you can use to interact with the web service. You can then create an instance of this proxy class and call its methods to consume the web service.
What is WCF and how is it different from .NET web services?
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. It provides a unified programming model for building secure and reliable transacted services that can integrate across platforms. Unlike .NET web services, which are limited to communicating over HTTP using SOAP, WCF services can communicate over any transport protocol (HTTP, TCP, MSMQ, etc.) and can use any message format (SOAP, binary, etc.).
How can I secure a .NET web service?
There are several ways to secure a .NET web service. One common method is to use SSL (Secure Sockets Layer) to encrypt the communication between the client and the service. This can be done by hosting the service on a secure server and accessing it via an HTTPS URL. Another method is to use WS-Security, a standard that provides message-level security by adding security information to the SOAP messages.
What is the role of the WebMethod attribute in .NET web services?
The WebMethod attribute is used to expose a method as a web service operation. When you apply this attribute to a method in your web service class, it becomes callable from remote clients. The WebMethod attribute also has several properties that you can use to control the behavior of the web service operation, such as BufferResponse, CacheDuration, and MessageName.
How can I handle exceptions in .NET web services?
Exceptions in .NET web services can be handled using the standard .NET exception handling mechanisms, such as try/catch blocks. However, it’s important to note that unhandled exceptions will be returned to the client as SOAP faults. To provide more control over the error information that is sent to the client, you can throw a SoapException, which allows you to specify the fault code, fault string, and detail element of the SOAP fault.
Can I use .NET web services with non-.NET clients?
Yes, .NET web services are based on standard protocols like HTTP and SOAP, which makes them interoperable with clients built on other platforms. As long as the client can send and receive SOAP messages over HTTP, it can consume a .NET web service.
How can I improve the performance of my .NET web service?
There are several ways to improve the performance of a .NET web service. One method is to enable HTTP compression, which can reduce the size of the SOAP messages and thus reduce the network bandwidth required. Another method is to use the BufferResponse property of the WebMethod attribute, which allows the server to start sending the response before the entire response is generated.
What is the role of the WebService attribute in .NET web services?
The WebService attribute is used to provide additional information about a web service, such as the namespace and the description. This information is used by the web services description language (WSDL) generator to create the WSDL document for the web service.
Can I use .NET web services with mobile applications?
Yes, .NET web services can be consumed by mobile applications. The process is similar to consuming a web service in a desktop or web application. You need to add a service reference to the project and then use the generated proxy class to call the web service methods. However, due to the limited resources of mobile devices, it’s important to optimize the web service for mobile consumption, for example by reducing the size of the SOAP messages or by using a more efficient message format like JSON.
Dimitrios is an expert .NET Architect and has written over a dozen articles covering various topics on .NET, and has been published on 4 Guys from Rolla, Dot Net Junkies, MSDN Academic Alliance, The Official Microsoft ASP.NET Site, and here on SitePoint.