Web Service
Web service is used to interact with one to another application through HTTP protocols. Web service gets hosted on only http protocol so, Web service does not allow interacting through another protocol as stp and other.
Steps to create a Web service
Step 1: Open Microsoft Visual Studio and go to File, then select new Web site then you will get the following:
Step 2: Select ASP.NET Empty Web site and change website name as your project, then click Ok button. Then we will get solution explorer as:
Step 3: Click on website with RM button and select add new item, then select web service and click on Add button
Step 4: Template will appear in Solution Explorer as
Step 5: Write the following code in WebService.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
-
-
-
-
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
-
-
- public class WebArth : System.Web.Services.WebService {
-
- public WebArth () {
-
-
-
- }
-
- [WebMethod]
- public int Add(int a,int b)
- {
- return a+b;
- }
- [WebMethod]
- public int Sub(int a, int b)
- {
- return a - b;
- }
- [WebMethod]
- public int Multiply(int a, int b)
- {
- return a * b;
- }
- [WebMethod]
- public int Divide(int a, int b)
- {
- return a / b;
- }
-
- }
This is all about create web service in our project. Now we will discuss about how to consume web service.
Step to Consume Web service
Step 1: Create a new website.
Step 2: Click on Website with RM button and select add new item then select Web form as:
*web form
Then click Add button.
Step 3: Design the Web form as per the requirement, then
Step 4: Add service reference to your web site through the following steps:
- Click on your Web Service with RM button and select Add service reference, then add service reference window will appear as:
- Click on Advanced button after that click on add Web References button
- Copy the URL from the URL of the browser.
- Past the copied URL address in the address textbox of service reference window. Click 'Go' button.
- Rename the Web reference name as required and then press Add Reference.
- The reference of the Webservice will appear in the Service Reference folder in solution explorer.
- Go to Web form page of your project and write the following code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using sr1;
- public partial class _Default : System.Web.UI.Page
- {
- WebArth obj = new WebArth();
- int a, b, c;
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void TextBox2_TextChanged(object sender, EventArgs e)
- {
-
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- a = Convert.ToInt32(TextBoxfirst.Text);
- b = Convert.ToInt32(TextBoxsecond.Text);
- c = obj.Add(a, b);
- TextBoxResult.Text = Convert.ToString(c);
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- a = Convert.ToInt32(TextBoxfirst.Text);
- b = Convert.ToInt32(TextBoxsecond.Text);
- c = obj.Sub(a, b);
- TextBoxResult.Text = Convert.ToString(c);
- }
- protected void Button3_Click(object sender, EventArgs e)
- {
- a = Convert.ToInt32(TextBoxfirst.Text);
- b = Convert.ToInt32(TextBoxsecond.Text);
- c = obj.Multiply(a, b);
- TextBoxResult.Text = Convert.ToString(c);
- }
- protected void Button4_Click(object sender, EventArgs e)
- {
- a = Convert.ToInt32(TextBoxfirst.Text);
- b = Convert.ToInt32(TextBoxsecond.Text);
- c = obj.Divide(a, b);
- TextBoxResult.Text = Convert.ToString(c);
-
- }
- }
Now run the application and you will get the correct output.
This is all about how to consume or work with Web service.