Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
Using Web services in ASP.NET
This introductory tutorial will show you how to easily use Web services in ASP.NET through an example Simple Online Product Warehouse Visual Studio project. It is structured as follows: the first section will introduce and present web services, then the second section will present the design part of our system.
In section 3, we will how to write web services and in section 4, we will conclude the tutorial and give some further research exercises. Our system is nothing else than a simple online product warehouse in which users can look at the availability of different products on the internet and decide whether to buy it or not. For the sake of simplicity and clarity, our products are stored as strings instead of a real database. However this will pan out talks for the final section of this tutorial.
As stated on the W3C website "Today, the principal use of the World Wide Web is for interactive access to documents and applications. In almost all cases, such access is by human users, typically working through Web browsers, audio players, or other interactive front-end systems. The Web can grow significantly in power and scope if it is extended to support communication between applications, from one program to another. "
A web service can be defined as a service available through the World Wide Web that makes use of the XML as message system and is not tied to any programming language nor operating systems (OS). Conceptually, Web services have many similarities with the web pages. However, there are some dissimilarities as well such as the Graphics User Interface (GUI) and the way the code underneath it works. This difference will become clear as we will progress in the tutorial. For now, we should note that web services do not have any GUI and are designed to be used by a program to deliver a service whereas web pages are designed to be used by a browser to be viewed by users.
Advocates of cross platform programming will be particularly keen of web services as they are completely independent of the OS or the programming language used to develop it. All that is necessary is that both server and client support the industry standard protocols HTTP, SOAP, and XML. HTTP is the protocol used by the Web. SOAP (Simple Object Access Protocol) is a lightweight, object-oriented protocol based on XML, which in turn is a cross-platform standard for formatting and organizing information.
The rationale of web services
In order to fully understand how web services work, it is vital to note that web services aim at providing services to clients over the world wide web. This is possible by the standard internet protocols used by the internet. Therefore, web services can be seen as functions that are called over the Internet by clients in order to be "served". This kind of function calls has the same characteristics as Remote Procedure Calls.
The components of web services are presented as follows: a web service consumer (i.e., a program that uses a particular web service, sometimes called the consuming program) makes a call to the web service. The consumer thinks it is talking directly to the web service over the Internet. Of course, this is just an illusion!
The actual call is being made to a proxy class which is local to the consumer. The proxy handles all the complex infrastructure of sending the request over the Internet to the server machine, as well as getting results back and presenting them to the consumer.
Writing a web service
If you are comfortable with writing web pages with Visual Studio .NET, then you should not be wary of this process.
At first, you should note that web pages and services are just text files. For people using Visual Studio .NET, you should note that it makes use of a tenet called code-behind which is a technique intended to separate visual content from programmatic content in web pages. As such, its use in web services is less imperative, since a web service does not have any visual content. However, Visual Studio .NET uses code-behind for every web project, whether visual or not, it gets used for web services as well. In fact, when using Visual Studio .NET to create web services, just as with web pages, code-behind is used by default.
Our Online Product Warehouse
Now that you understand how web services work, it is time to put everything all together and write some useful stuff.
For the sake of clarity, we will be using Visual Studio .NET 2008 and C# as this couple of tools is probably the best thing available at the moment.
Our system will ask users to input a product name in a specified text box and when click on a Search button, the system will return both the price and the number of items left.
The user can also request all available products. The final product is shown at the following screenshot.
The system is made of two components: a server which hosts the web service itself and a client using the web service.
The corresponding code is contained in a Visual Studio Solution file which is made of 2 projects:
- Rac Job: an ASP.NET Web Service Application housing the server
In order to access to services, the server must provide an interface to clients. These are contained in the following functions:
Which are both self explanatory.
Our fake database is nothing else than a string containing all products, their price and the number of items remaining. This information can be encoded a 2D string:
On the client side, we need to define two buttons in the GUI from which we will request services to the server. The handlers of these buttons are as follows:
requests the price of a given product
That is it for our simple online product warehouse!
Conclusion and future work
Well, throughout this tutorial, we have learn what and how web services work. Now you can be proud and hopefully you have grasped enough to dig into the subject.
Future works can be focused on building a database to host products. As I said from the very first section of this tutorial, in a real world application, a database must be used to stored these products as it is more flexible and easy-to-use. Hard coding everything is definitely not a good idea in this case. What about if someone want to add another product? Anyway, the main purpose of this tutorial was to show you how you can quickly create your own web service with Visual Studio .NET. Linux avid users will be probably not be happy of my choice of the IDE but dudes in this case, VS will save your valuable time and resources.
Happy coding and learning!
comments powered by Disqus