.NET Framework Class Library
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)
Syntax

Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.Method)> _
Public NotInheritable Class WebMethodAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As WebMethodAttribute
C#
[AttributeUsageAttribute(AttributeTargets.Method)]
public sealed class WebMethodAttribute : Attribute
Visual C++
[AttributeUsageAttribute(AttributeTargets::Method)]
public ref class WebMethodAttribute sealed : public Attribute
JScript
public final class WebMethodAttribute extends Attribute
Remarks

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.

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

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.

Visual Basic
<%@ WebService Language="VB" Class="Util"%>

Imports System
Imports System.Web.Services

Public Class Util
    Inherits WebService

    Public Function GetUserName() As String
        Return User.Identity.Name
    End Function    

    <WebMethod(Description := "Obtains the Server Machine Name", _
        EnableSession := True)> _
    Public Function GetMachineName() As String

        Return Server.MachineName
    End Function
End Class

C#
<%@ 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",
       EnableSession=true)]
       public string GetMachineName() {
          return Server.MachineName;
       }
    }

Inheritance Hierarchy

System..::.Object
  System..::.Attribute
    System.Web.Services..::.WebMethodAttribute
Thread Safety

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

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
See Also

Reference

Tags :


Page view tracker