In this article we will discuss how to create a Web Service, deploy it and use it in our application.
How to Create a Web Service
Follow this procedure to create a simple Web Service.
Step 1
First we will click on "File" -> "New" -> "Project..." -> ASP.NET Web Service Application like this:
Step 2
The Service1.aspx.cs page will be opened; here we will write the following code:
namespace CalcWebService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public double Add(int a, int b, int c)
{
int sum;
sum = a + b + c;
return sum;
}
}
}
This namespace is the default namespace, we can change it as needed. Here we create a WebMethod (Add) , to define the functionality of the WebService. We can also send and receive messages using the WebMethods over the internet.
Step 3
Now we will execute the WebService (press F5 Key).
The output will be:
Now we will click on Add:
Now we enter some values in it:
Now we will click on the Invoke Button. The result will be shown in a new page like this:
Deploying The Web Service
Now we will look at this, how to deploy the web service, in other words how to make this web service available for the other web applications over the internet.
Step 1
First we will open our Web Service in the Visual Studio and then we will click on "File" -> "New" -> "Project..." like this:
A window will be shown; here we will select the Other Project Types and then Setup Project:
When we will click the Ok Button the following window will be shown:
Here we click on "Add" -> "Project Output". After that the following window will be shown where we select:
After that right-click on the CalcSetup and then select the Build Option like this:
So when we buid the project the CalcSetup folder populates the two folders (Debug and Release).
Now double-click on the Debug folder and run the exe file:
Now click on the Next Button:
After that install the CalcSetup, after the installation it will be available over the internet.
How to use this WebService in an Application
In order to use this WebService in an application, we create a project that contains this Web Service. After that we create a .aspx page Adddata.aspx.
Here we will use the following controls to show our data:
<table >
<tr>
<td class="style2">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td class="style3">
First Value</td>
<td>
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Second Value</td>
<td>
<asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Third Value</td>
<td>
<asp:TextBox ID="txtThird" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Button ID="btnAdd" runat="server" onclick="Button1_Click" Text="Add" />
</td>
</tr>
<tr>
<td class="style3">
Output</td>
<td>
<asp:TextBox ID="txtOutput" runat="server"></asp:TextBox>
</td>
</tr>
</table>
So when we click on the Add Button the output will be shown in the TextBox. For this, first we add the Web Reference in our project, for this right-click on the CalcWebService Project and Select Add Web Reference like this:
The following window will be shown:
Here we will select Web Services in this solution:
When we click on the Service1:
Now we will click on the Add reference Button. So the output will be:
Now we will write the code on the Add Button to use this Web service like this:
protected void btnAdd_Click(object sender, EventArgs e)
{
localhost.Service1 ws = new CalcWebService.localhost.Service1();
ws.EnableDecompression = true;
double sum = ws.Add(System.Convert.ToInt16(txtFirst.Text), System.Convert.ToInt16(txtSecond.Text), System.Convert.ToInt16(txtThird.Text));
txtOutput.Text = sum.ToString();
}
The output will be: