Creating and consuming a web service in C# / .NET

Creating a web service in Visual Studio

Create a new Visual Studio project

Use .NET 3.5

Right click on the project and select Add > New Item > Web Service (ASMX)

See that the following code gets automatically generated in WebService.asmx.cs

WebService.asmx.cs

using System.Web.Services;

namespace WebService
{
   /// <summary>
   /// Summary description for WebService
   /// </summary>
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
   // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
   // [System.Web.Script.Services.ScriptService]
   public class WebService : System.Web.Services.WebService
   {

      [WebMethod]
      public string HelloWorld()
      {
         return "Hello World";
      }
   }
}

On compiling and running the project you are directed to a web browser that points to your service containing the single ‘HelloWorld’ method:

And when you click on the ‘HelloWorld’ link you are directed to the page to invoke the selected operation:

Clicking Invoke then directs you to the XML file:

Add two additional services, Square and SquareRoot to WebService.asmx.cs:

WebService.asmx.cs

using System;
using System.Web.Services;

namespace WebService
{
   /// <summary>
   /// Summary description for WebService
   /// </summary>
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
   // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
   // [System.Web.Script.Services.ScriptService]
   public class WebService : System.Web.Services.WebService
   {

      [WebMethod]
      public string HelloWorld()
      {
         return "Hello World";
      }

      [WebMethod]
      public double Square(double x)
      {
         return x * x;
      }

      [WebMethod]
      public double SquareRoot(float x)
      {
         return Math.Sqrt(x);
      }
   }
}

Rebuild and re-run your project and notice that two additional services are displayed:

Click on one of the new examples, SquareRoot for example, and invoke this:

Giving use the answer in XML format as shown:

Consuming the web service

Create a new Console Application in your solution

Right click your project and select Add > Service Reference…

Rename the namespace if you wish:

Click on the Advanced button.

Click Add Web Reference

Click the link that says to browse to services in this solution.

Observe that it discovers the web service we earlier created for this solution:

Click on this service and see that the range of service methods is displayed:

Rename the Web reference name to your preference:

Click Add Reference.

Observe the Web References folder containing WebServices is created as shown:

Set the console application as your startup project:

Update Program.cs in your console application to consume the referenced web service.

In this example to find and display the square root of 64:

Program.cs

using System;
using WebServiceConsumer.WebServices;

namespace WebServiceConsumer
{
   internal static class Program
   {
      private static void Main(string[] args)
      {
         var service = new WebService();

         Console.Write("Square root of 64 = {0}", service.SquareRoot(64));
      }
   }
}

Giving the following console output when run:

Leave a Reply