Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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.

For a list of all members of this type, see WebMethodAttribute Members.

System.Object
   System.Attribute
      System.Web.Services.WebMethodAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Method)>
NotInheritable Public Class WebMethodAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Method)]
public sealed class WebMethodAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Method)]
public __gc __sealed class WebMethodAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Method)
class WebMethodAttribute extends Attribute

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.

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.

Example

[Visual Basic, C#] 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;
       }
    }

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.Services

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Web.Services (in System.Web.Services.dll)

See Also

WebMethodAttribute Members | System.Web.Services Namespace | TransactionOption | WebService

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft