| I
have already discussed in my First Article XML
Web Services Torn Apart - 1 what Web Services
is and how it works. Now we will create an XML Web Service
and a client for accessing the service. As I have told
all about Web Service in the First part of this article,
I don't want to go again into all those explanations.
So our task here is to create a Web Service and then a
client for accessing that service. Here we go for creating
a Web Service. We will be creating it with C#.
Creating
an XML Web Service
1.
Open Microsoft
Visual Studio.NET 2003.
Microsoft Visual Studio.NET has a common IDE
for all the members of the visual studio family.
2.
From the File
menu select the New option, from that select the Project
option.
3.
Now you will see a New
Project dialog box. In the dialog box
you can see two sections Project
Types & Templates.
From the Project Types select Visual C# Projects &
from the Templates select ASP.NET Web Service.
4.Now
we have to give a name for our Web Service. The name
will be BasicMathOprWS.
The name field may be disabled. So what we will do is
below that you will see a location
Field. In that you will find the default location as
"http://localhost/WebService1
". Edit the line to make it like this "http://localhost/BasicMathOprWS".Click
OK.
5.Now
Visual Studio .NET will create a project in the specified
name for you.
6.
In the Solution
Explorer you will see many file &
you can see a file Service1.asmx.This
is the web service file. The Web Service file will always
have an extension ".asmx".
7.
Double click on the design form to go to the code view.
Alternatively in the Solution
Explorer you can right click on the
Service1.asmx
file to select the View
Code option form the menu to go to the
Service1.asmx.cs
code file or another method is as Solution
Explorer hides the code behind file
by default click on the Show
All Files icon to get the Service1.asmx.cs
file.
8.Now
we have to implement code for the Xml Web Service which
we are creating.
9.Insert
the following code just before the Service
1 class declaration.
[System.Web.Services.WebService
(Namespace ="http://servername/xmlwebservices/",
Description = "Some descriptive text could go here.")]
10.Next
we have to add code for our Web Service functionality.
Our Web Service will have four functionalities namely
Addition, Subtraction, Multiplication, Division. So
for this we have to add four functions Addition, Subtraction,
Multiplication, and Division.
11.To expose a function as a part of our Web Service
we have to add an attribute called WebMethod.
Please note that it is compulsory to place a WebMethod
attribute before each
public function you wish to expose as
a part of the XML Web Service. Add the following code
in the Service
1 class to expose our functionalities.
[WebMethod(Description="This
method does the Addition ")]
public double Addition(double
dFirstNum,double dSecondNum)
{
return (dFirstNum + dSecondNum);
}
[WebMethod(Description="This
method does the Subtraction")]
public
double Subtraction(double
dFirstNum,double dSecondNum)
{
return (dFirstNum - dSecondNum);
}
[WebMethod(Description="This
method does the Multiplication")]
public
double Multiplication(double dFirstNum,double
dSecondNum)
{
return (dFirstNum * dSecondNum);
}
[WebMethod(Description="This
method does the Division")]
public
double Division(double
dFirstNum,double dSecondNum)
{
return (dFirstNum / dSecondNum);
}
12.Now you have added the code. Next in the solution
explorer right click on the Service1.asmx
file and select Set
As Start Page.
13.Build
the Project.
14.Now
we have created the XML Web Service. But our next question
is how we can make it available to others? The Web Service
that we made can be made available to others by deploying
it to a server that is available to others. For this
either you can create a Web
Setup Project or copy all the necessary
file to the destination server. But here we will do
it in our development machine itself. Since we use our
development machine there is no need to go for a Web
Setup project but still I will show you how to make
a Web Setup project so that you can learn it.
15.
From the File
menu select the Add
Project option, from that select the
New
Project option.
16. From the Add
New Project dialog box from Project
Types select Setup
and Deployment Projects & from the
Templates
section select Web
Setup Project.
17.
In the Name Field type the name as BasicMathOprWSWebSetup.
18.
Click OK.
19.
Now you will see a File
System Editor window with two sections.
In the left section you will fine a folder named Web
Application Folder.
20.Right
click on the Web
Application Folder and select Add
and then select Project
Output.
21.
Now you will find a dialog box name Add
Project Output Group. From this select
Content
Files, Primary
Output & Debug
Symbols.
22.
Go to the Solution
Explorer right click on the "MathOprWSWebSetup"
project and Build
it. This creates an installer for our Web Service in
your local project directory.
23.
Now in your local project directory you can find a folder
"MathOprWSWebSetup".
In that folder you can find two other folders debug
& release. Open the debug folder and in that you
can find MathOprWSWebSetup.msi.
Double click on it to install the web service on your
machine. This creates a folder MathOprWSWebSetup
with all the necessary files in your "C:\Inetpub\wwwroot"
directory.
Now
we have created a Web Service & we installed it
for consuming it from clients. So now we have to create
a client application to consume our Web Service.
Creating
an XML Web Service Client
1.
Open Microsoft
Visual Studio.NET 2003.
2.
From the File
menu select the New
option, from that select the Project
option.
3.
Select Visual
C# Projects from project types &
ASP.NET
Web Application form templates.
4.
The name field may be disabled so in the location field
replace the "WebApplication1" with MathOprWSClient.
5.
Click OK
|