Defining XML Web Service Methods

Methods of a class implementing an XML Web service do not automatically have the ability to be communicated with over the Web, but with XML Web services created using ASP.NET, it is very simple to add that capability. To add this capability, apply a WebMethod attribute to public methods. Methods of an XML Web service that can be communicated with over the Web are called XML Web service methods.

XML Web service methods are a key part of the messaging infrastructure employed by XML Web services. That is, a client and an XML Web service communicate using messages, specifically SOAP messages, by default. Clients send a SOAP request to an XML Web service and an XML Web service method returns a SOAP response. XML Web services define the type of messages it accepts using operations, as defined by Web Services Description Language (WSDL). These operations correlate to each of the XML Web service methods within an XML Web service. Even though each of these XML Web service methods are defined in ASP.NET using a method of a class, it is important to realize that the data that is eventually communicated over the network must be serialized into XML. As such, it is important to remember that XML Web services are not a replacement for DCOM, but rather a messaging infrastructure for communicating across platforms using industry standards.

To declare an XML Web service method

  1. Declare an XML Web service, adding the @ WebService directive. For more information, see Declaring an XML Web Service.
  2. Add public methods to the class implementing the XML Web service.
  3. Apply the WebMethod attribute to the public methods you want to be mapped to operations.

The following code example has two public methods, one of which is an XML Web service method. The Multiply method is an XML Web service method, as it has a WebMethod attribute applied to it.

<%@ WebService Language="C#" Class="Util" %>
    using System;
    using System.Web.Services;
    public class Util: WebService 
     {
       public int Add(int a, int b) 
       {
          return a + b;
       }
    
       [ WebMethod]       public long Multiply(int a, int b) 
       {
          return a * b;
       }
   }
[Visual Basic]
<%@ WebService Language="VB" Class="Util" %>
    Imports System
    Imports System.Web.Services
    Public Class Util 
       Inherits WebService

       Public Function Add(a As Integer, b As Integer) As Integer
          Return a + b
       End Function
    
       < WebMethod()> _       Public Function Multiply(a As Integer, b As Integer) As Long
          Return a * b
       End Function
   End Class

See Also

WebMethodAttribute Class | Building XML Web Service Using ASP.NET | Declaring an XML Web Service