Creating Your First Web Service Using Asp.net with visual studio 2010
- Start visual studio 2010
- Start a new website.
- Select Asp.net web service in the new website dialog box and click OK.
- In the service.cs file type a web method as follows
- Helloworld web method is automatically generated (no:1 in image)
- Here I have added a new web method called "Add" which takes two parameters(no:2 in image)
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public int Add(int a, int b)
{
return a + b;
}
- Then run the web service. you will see the list of operations supplied by the service
- no:1 shows the helloworld method and no:2 shows our mehod "Add" and no:3 shows the link to service discription of the web service
- When you click no:1 and no:2 as shown above you will get the web methods respectively
- Then we can click invoke button to check the web method. If there are any required field must be filled-these fields are the parameters of web method.
- After clicking invoke button we get the xml result of our service if service is works properly as follows.
- For Helloworld web method;
- For Add web method
- Now you are done with asp.net web service creation
Consuming Your First Web Service Using Asp.net with visual studio 2010
- Start another visual studio 2010 instance.
- Create a new web application.
- Then create the GUI in the design of default.aspx file
- Then double click the Add Numbers or go to the button click event of Add Numbers button
- Then run your first web service and copy the url of web service.
- Then right click on solution name in solution explorer.
- Select Add Web Reference

- Then paste the url in the url box of the Add Web Reference dialog box and hit enter key.
- Then click the web method you want from the list appear
- Then you will see the localhost web reference is added
- Then rename the local host to one you like-I use "MyService"
- Then add the name space to default.aspx.cs
- Type the following code in the button click event method
int a=Convert.ToInt32(TextBox1.Text);
int b=Convert.ToInt32(TextBox2.Text);
MyService.Service s = new Service();
int ans=s.Add(a, b);
Label1.Text = "The Sum is :" + ans;
- In image
1-get the values from the text boxes and convert to int
2-create an instance of the service class of web service
3-call the web service-web method with parameter passing
4-attach result to the lable
- Then run your web application
- You will get a interface similar to this
- Give input values and click Add Numbers button
- It will send to the web service and web service will return the sum
- Then your application will show the result.
- Now you are consuming your own web service
Coool...
Try this and then Check what happens behind the scene-Good Luck.
good one....keep posting
ReplyDelete