How To Create A Web Service Project In .NET Using Visual Studio

Introduction
 
In this demo, today, we will try to create a web service project. We will also learn how to consume the web service in our project. Basically, at the end of this project, you should be able to use the web methods created in web service in your code. So, if you are able to achieve this, you are good with this concept. Let's go through each step one by one and see how it happens.
 
What is a Web service?
 
I hope you know the meaning of web service before you execute the demo. A web service is nothing but a software application which runs on the web having some exposed web methods which other applications can use over HTTP/ HTTPS protocols using technologies such as XML, SOAP, WSDL, and UDDI. Here in this demo, we will create one such web service and we will try to use its web methods. We will do all of this in a single web project but you can try it in different projects on the same machines as well as different projects on different machines. It should work.
 
Step 1
 
Click on File >> New >> Project as given below.
 
How To Create A Web Service Project In .NET Using Visual Studio
Once you click on Project, you will see the following pop-up window.
 
How To Create A Web Service Project In .NET Using Visual Studio
 
Step 2
 
Here, choose ASP.NET Web Application (.NET Framework) and give it a name as I have given - WebServiceProject. Click on OK.
 
Select the Empty template.
 
It creates a solution having the following solution structure.
 
 How To Create A Web Service Project In .NET Using Visual Studio
Right-click the project.
 
How To Create A Web Service Project In .NET Using Visual Studio
 
Step 3
 
Once you click on the New Item, choose Web Service and give it a name as given below.
 
How To Create A Web Service Project In .NET Using Visual Studio
 
Step 4
 
Now, write the following code in WebService.asmx file.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6.   
  7. namespace WebServiceProject  
  8. {  
  9.     /// <summary>  
  10.     /// Summary description for WebService  
  11.     /// </summary>  
  12.     [WebService(Namespace = "http://tempuri.org/")]  
  13.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  14.     [System.ComponentModel.ToolboxItem(false)]  
  15.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  16.     // [System.Web.Script.Services.ScriptService]  
  17.     public class WebService : System.Web.Services.WebService  
  18.     {  
  19.   
  20.         [WebMethod]  
  21.         public string HelloWorld()  
  22.         {  
  23.             return "Hello World";  
  24.         }  
  25.         [WebMethod]  
  26.         public int Add(List<int> listInt)  
  27.         {  
  28.             int result = 0;  
  29.             for(int i =0; i< listInt.Count; i++)  
  30.             {  
  31.                 result = result + listInt[i];  
  32.             }  
  33.   
  34.             return result;  
  35.         }  
  36.     }  
  37. }  
Here, WebMethod HelloWorld comes by default when you create a web service. You can change its implementation if you want. We have implemented another method which can take a list of integers as input and will give you some of all the lists of integers given as input.
 
Now, if you run this project and point the URL to WebService.asmx, then you will get the following result, which indicates that your web service has been created.
 
How To Create A Web Service Project In .NET Using Visual Studio
Now you can find all the descriptions of services written on this page having the list of methods which are available via this web service. You can also see the service description to get a better picture. If you will click methods, then you will see its soap request response structures and for simple generic methods, you will also see an option to invoke methods.
 
How to consume this Web service -- there are multiple ways for doing that. We will here try to use it via a web reference method. We will add Web reference of this service in our project for consumption.
 
How to add web service reference in Project?
 
For adding Web reference of this service in the project, right click on the project and click "Add Service Reference". Once you do that you will see the following popup window.
 
 How To Create A Web Service Project In .NET Using Visual Studio
 
So here we have entered the path to the web service and once you click on Go you will see the service structure like this. You can also see methods exposed by click on WebService over here. Just give it a namespace name which you want to use. For us, we are using ServiceReference1. Click OK and it will add service reference of this web service in your project.
 
Now how to use this web service?
 
For this, I will add a webform page to our project. Here I have added WebForm.aspx having a button and a label. What I want to do here is on click of a button in this webform I should get the sum of an integer list using our web service method. So code looks like below in WebForm.aspx.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebServiceProject.WebForm" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" style="height: 26px" /><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
  13.         </div>  
  14.     </form>  
  15. </body>  
  16. </html>  
In WebForm.aspx.cs, I have written a handler like this.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using WebServiceProject.ServiceReference1;  
  8.   
  9.   
  10. namespace WebServiceProject  
  11. {  
  12.     public partial class WebForm : System.Web.UI.Page  
  13.     {  
  14.         protected void Page_Load(object sender, EventArgs e)  
  15.         {  
  16.   
  17.         }  
  18.   
  19.         protected void Button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             WebService webService = new WebService();  
  22.             List<int> lstIntegers = new List<int> { 5, 6, 7 };  
  23.             Label1.Text =  "Output of WebService: " +webService.Add(lstIntegers).ToString();  
  24.   
  25.         }  
  26.     }  
  27. }  
So now, if you run this project and point to this webform, you will get the sum of all these hardcoded 3 integers' list using the Add method. Output of this comes as below after clicking the button on the page.
 
How To Create A Web Service Project In .NET Using Visual Studio
The task is  for this concept.
 
Conclusion
 
We have understood using the steps above how to create a web service and how to use a web service using the addition of web service in C# web project in visual studio. There are other ways also available to consume this via SOAP and other web mechanisms. Do run all these steps and you should not be getting any issues in setting all of the above things on your machine.


Similar Articles