.NET Web Services Support

Consider the SimpleCalculator web service, shown in Example A-1, which provides the four basic arithmetic operations.

Example A-1. The SimpleCalculator web service

using System.Web.Services;

[WebService(Namespace="http://CalculationServices",
            Description = "The SimpleCalculator Web Service provides the
                           four basic arithmetic operations for integers.")]
public class SimpleCalculator
{
   [WebMethod]
   public int Add(int argument1,int argument2)
   {
      return argument1 + argument2;
   }
   [WebMethod]

   public int Subtract(int argument1,int argument2)
   {
      return argument1 - argument2;
   }
   [WebMethod]
   public int Divide(int argument1,int argument2)
   {
      return argument1 / argument2;
   }
   [WebMethod]
   public int Multiply(int argument1,int argument2)
   {
      return argument1 * argument2;
   }
}

Using .NET, all you have to do to develop a web service is add the WebMethod attribute to the methods you wish to expose as web services—.NET will do the rest. The WebServiceAttribute attribute is optional, but you should use it. The attribute is defined as:

    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]
    public sealed class WebServiceAttribute : Attribute
    {
       public WebServiceAttribute();
       public string Description{get; set;}
       public string Name{get; set;}
       public string Namespace{get; set;}
    }

WebServiceAttribute lets you specify a web service namespace that contains your service, used like a normal .NET namespace to reduce collisions. If you don’t specify a namespace, Visual Studio 2005 uses ...

Get Programming .NET Components, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.