Skip to Main Content
Programming .NET Components, 2nd Edition
book

Programming .NET Components, 2nd Edition

by Juval Lowy
July 2005
Intermediate to advanced content levelIntermediate to advanced
644 pages
17h
English
O'Reilly Media, Inc.
Content preview from Programming .NET Components, 2nd Edition

.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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Windows Forms Programming in C#

Windows Forms Programming in C#

Chris Sells
Metaprogramming in .NET

Metaprogramming in .NET

Jason Bock, Kevin Hazzard
.NET Windows Forms in a Nutshell

.NET Windows Forms in a Nutshell

Ian Griffiths, Matthew Adams

Publisher Resources

ISBN: 0596102070Supplemental ContentErrata Page