How to: Create a Web Service Method

 Windows Communication Foundation Services and ADO.NET Data Services

When you create a Web service in managed code, you indicate the methods that are available through that Web service by placing the WebMethod attribute before the method declaration of a Public method. Private methods cannot serve as the entry point for a Web service although they can be in the same class and the Web service code can call them. The WebMethod attribute must be applied to each public method that is available as part of the Web service. For more information, see How to: Use the WebMethod Attribute.

The procedures below assume that you are working with public methods of a class that is the implementation of a Web service. For more information, see Code Model for Web Services in Managed Code.

To create a Web service method

  1. Double-click your .vb or .cs file (for example, Service1.vb or Service1.cs) in Solution Explorer to open the code editor.

  2. Add a public method to the class specified in the Class property of the .asmx file's WebService processing directive as shown below:

    Public Class Service1
        Inherits System.Web.Services.WebService
        <System.Web.Services.WebMethod()> _
        Public Function ConvertTemperature(ByVal dFahrenheit As Double) _
                                           As Double 
            ConvertTemperature = ((dFahrenheit - 32) * 5) / 9 
        End Function 
    End Class
    
    public class Service1 : System.Web.Services.WebService
    { 
        [System.Web.Services.WebMethod()]
        public double ConvertTemperature(double dFahrenheit)
        {
           return ((dFahrenheit - 32) * 5) / 9;
        }
    }
    

    Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the Web service. For more information, see Code Model for Web Services in Managed Code.

See Also

Reference

WebMethodAttribute

Other Resources

Creating Web Services in Managed Code