O'Reilly logo

Programming ASP.NET, 3rd Edition by Dan Hurwitz, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

WebMethod Attribute

As explained previously, a web service is defined by a WebService class. It is not necessary for the WebService class to expose all of its methods to consumers of the web service. Each method you do want to expose must do the following:

  • Be declared as public.

  • Have the WebMethod attribute placed before the method declaration. (The WebMethod attribute comes from the WebMethodAttribute class, which is contained in the System.Web.Services namespace.)

As you saw in the previous examples in this chapter, the basic WebMethod attribute looks something like the highlighted code in the following snippet:

            
    [WebMethod]
    public string GetName(string StockSymbol)

The WebMethod attribute has properties that are used to configure the behavior of the specific web method. Here is the syntax:

    [WebMethod(PropertyName=value)]

PropertyName is a valid property accepted by the WebMethod attribute (these are described below), and value is the value to be assigned to that property. If there are multiple WebMethod properties, separate each property/value pair with a comma within a single set of parentheses as in this example:

    [WebMethod(BufferResponse=false, Description="Sample description")]

The following sections describe the WebMethod properties.

The BufferResponse Property

By default, ASP.NET buffers the entire response to a request before sending it from the server to the client. Under most circumstances, this is the optimal behavior. However, if the response is very lengthy, you might want ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required