Monday, June 9, 2008

A Simple Web Service

Today ,we will learn how to create a web service . A web service is supposedly much safer and faster and hence very important to any website project .

*To create a web service go to add a new item .
*Add a Web service (.asmx) file to your solution .
*In this File there is a default web method called "Hello World" which returns hello world .
*You can write your methods out here and declare them as a web methods .
*If you dont want to write all the coding on the .asmx page you can write the code in a class file and call those methods in a webmethod

*Now to test the service debug this application and in the window u will see the name of the methods that you have defined .
*Click on the method you want to test ,this will open a window which will have the consist of the specific webmethod and an "Invoke" button .
*Press the Invoke button and You will be able to see the results in an XML file .

*Now to add this service to Your web site application , follow these steps :
1.Add a web reference to your project this will give the below options
  • Web services in the solution
  • Web services on the local machine
  • Browse UDDI Servers on the local network
2.You can select the first option if your webservice is in the same solution else choose the second option .
3. Change the namespace for your service as per your requirements ,by default it is given a name localhost.

4.Now in your webpage (eg:Default.aspx) write the following code
localhost.WebService ws = new localhost.WebService();
Label1.Text= ws.HelloWorld();//I have included a Label also to show the results

That is all you have to do to include a webservice to your website .

No comments: