The term "web service" has been used quite frequently lately: you hear people saying how good web services are and how they will dominate the future of software development, but what exactly are web services and how can we create them? In this article James shows us how to build two web services and also unravels all of the lingo surrounding web services.
Building XML Web Services Using C# and ASP.NET - Real world application (Page 5 of 6 )
Well, we've learnt all of the fundamentals for building web services. It's time to put what we've learnt into practice by designing a real world example. The example application we're about to create will not contain properties, because Microsoft recommends a web service be stateless whenever possible.
We are going to make a stripped-down version of Passport. Our version will contain seven methods:
bool Authenticate (string username, string password): This method will authenticate a user and return true if authenticated and false if not.
bool AddUser (string username, string password, string name, string email): This method will add a user to the database. If successful, the method will return true, if not the method will return false.
bool DeleteUser (string username): Will delete a user from the database. If successful the method will return true, if not the method will return false.
bool EditUser (string username, string name, string email): This method will edit the user information. If successful the method will return true, if not the method will return false.
bool ChangePassword (string username, string password): This method will change a user’s password. If successful the method will return true, if not the method will return false.
string ReturnName (string username): this method returns a users name.
string ReturnEmail (string username): this method returns a users email.
Our example makes use of an SQL server database. Use the following TSQL in query analyzer to create our database (In our example I will assume that SQL Server is installed on the same machine as where the web service will reside):
CREATE DATABASE minipassport
GO
CREATE TABLE Users (
UserName varchar (10) Primary Key NOT NULL ,
Name varchar (50) NOT NULL ,
EMail varchar (100) NOT NULL ,
Password varchar (10) NOT NULL
) ON PRIMARY
GO
The code for our web service looks like this:
<%@ WebService class = "miniPassport" Language="C#" Debug = "true"%>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
[WebService(Name ="Mini Passport", Description="Web Service to Authenticate and Manage Users", Namespace = "devArticles")]
As you can see, there's nothing difficult about our code. It's composed from what we've covered throughout this article. If you add your own functionality and make it available on the web (or even register it on UDDI), then it's a complete authentication web service. This will allow other sites to incorporate our demo login system and centralize user information.