How to create a web service in .NET

In this blog we create a Web Service project in Visual Studio.

Quick steps

  • In Visual Studio, create a new ASP.NET Web Application (.NET Framework). Choose the empty template.
  • Add a new item to your project. Choose Web Service (ASMX) and name it “Service.asmx”.
  • Copy and paste the example code into the Service.asmx file.
  • Publish the web service to Internet Information Server.
namespace WebServiceProject  
{  
    /// <summary>  
    /// Use the Get Insult method to retrieve a message 
    /// </summary>  
    [WebService(Namespace = "http://tempuri.org/")]  
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
    [System.ComponentModel.ToolboxItem(false)]  
    [System.Web.Script.Services.ScriptService]  
    public class WebService : System.Web.Services.WebService  
    {  
        [WebMethod(Description = "Randomly retrieve Shakespeare Insults")]
        public string GetInsult()
        {
            string one, two, three = “”;
            return String.Format("Thou {0} {1} {2}", one, two, three);
        } 
}  

When you run this project and point the URL to Service.asmx, you should get the following result.

This is an overview page of all services with their description. Click on a method, you will see its soap request-response structure and an option to invoke the method.