WebMethodAttribute Class

Adding this attribute to a method within an XML Web service created using ASP.NET makes the method callable from remote Web clients. This class cannot be inherited.

Namespace: System.Web.Services
Assembly: System.Web.Services (in system.web.services.dll)

public sealed class WebMethodAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Method) */ 
public final class WebMethodAttribute extends Attribute
public final class WebMethodAttribute extends Attribute
Not applicable.

Methods within a class that have this attribute set are called XML Web service methods. The method and class must be public and running inside an ASP.NET Web application.

Walkthrough: Creating and Using an ASP.NET Web Service in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio

In the example below the method GetMachineName can be remotely called across the Web, because it has a WebMethodAttribute. GetUserName cannot be called remotely, because it does not have a WebMethodAttribute, even though it is public.

<%@ WebService Language="C#" Class="Util"%>
    using System;
    using System.Web.Services;
    public class Util: WebService {
       public string GetUserName() {
          return User.Identity.Name;
       [ WebMethod(Description="Obtains the Server Machine Name",
       public string GetMachineName() {
          return Server.MachineName;


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions