Xml Web Services - Tone Apart - II
 
Author : Thomas Kurian Ambattu
 
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

6. In the Solution Explorer click on WebForm1.aspx.In the design view drag 3 labels, two text boxes and four buttons. Place it as shown in the following figure.


7. Set the property of each as shown in the following table
Item Text ID
Label1 First Label1
Label2 Second Label2
Label 3 Don't type any text Label3
TextBox   txtFirst
Text Box   txtSecond
Button Add btnADD
Button Subtract btnSUB
Button Multiply btnMULT
Button Divide btnDIV

8. Now we have to add a Web Reference so that the web service we created can be accessed by this client application. For this go to the Project menu. Select the Add Web Reference option. You will get an Add Web Reference dialog box. In the URL path give the address as " http://localhost/mathoprwswebsetup/Service1.asmx " or you can browse from the " Web Services on the Local Machine " option from the home page. In the Web Reference Name field type " BasicMathOpr " and click on the button Add Reference.

Note: Some times when you give the URL it is likely that you get errors with a page displaying some server error, this could be because of your ISS configuration settings. If you get an error similar to what I said just now see that the ISS is properly installed and the configurations are correct. Another error could be because of your Virtual Directory in IIS to rectify the error do the following procedure.

Go to Administrative Tools. Select Internet Services Manager you will get a window Internet information Services In the left window expand your machine name to get Default Web Sites and further expand this so that u get a list of folders and find out MathOprWSWebSetup right click on it and choose Properties. Now you get another Dialog box with tabs. Click on the Directory tab and somewhat after the middle portion you can see a section Application Settings and click on the Create button so that you get the Application Name field enabled and you get the an Application Name there by Default. Click OK and close the window Internet information Services window.


9. In the Solution Explorer right click on the WebForm1.aspx page and select Set As Start Page.

10. Double click on the Button Add and add the following code.
try
{
BasicMathOpr.Service1 ws = new BasicMathOpr.Service1();
double dFirstNumber = Convert.ToDouble(txtFirst.Text);
double dSecondNumber = Convert.ToDouble(txtSecond.Text);
double dResult = ws.Addition(dFirstNumber,dSecondNumber);
Label3.Text = dResult.ToString();

}
catch
{
Label3.Text = "Failed to do the Operation ";
}


11. Double click on Subtract button and add the following code
try
{
BasicMathOpr.Service1 ws = new BasicMathOpr.Service1();
double dFirstNumber = Convert.ToDouble(txtFirst.Text);
double dSecondNumber = Convert.ToDouble(txtSecond.Text);
double dResult = ws.Subtraction(dFirstNumber,dSecondNumber);
Label3.Text = dResult.ToString();
}
catch
{
Label3.Text = "Failed to do the Operation ";
}

12. Double click on Multiply button and add the following code
try
{
BasicMathOpr.Service1 ws = new BasicMathOpr.Service1();
double dFirstNumber = Convert.ToDouble(txtFirst.Text);
double dSecondNumber = Convert.ToDouble(txtSecond.Text);
double dResult = ws.Multiplication(dFirstNumber,dSecondNumber);
Label3.Text = dResult.ToString();
}
catch
{
Label3.Text = "Failed to do the Operation ";
}

13. Double click on Division button and add the following code
try
{
BasicMathOpr.Service1 ws = new BasicMathOpr.Service1();
double dFirstNumber = Convert.ToDouble(txtFirst.Text);
double dSecondNumber = Convert.ToDouble(txtSecond.Text);
double dResult = ws.Division(dFirstNumber,dSecondNumber);
Label3.Text = dResult.ToString();
}
catch
{
Label3.Text = "Failed to do the Operation ";
}

14.Now Build the Project and Execute it.

15.Test Application by giving the values and Enjoy .. ! ! !

 
 
     
 
   
 Copyright © 2003 NETKidoos.com All rights reserved Terms Of Use
Best viewed in 1024 X 768, IE 5.0 and above