Webservices – WebMethod Attributes…..

The WebMethod Attribute

Unlike the WebServiceAttribute, the WebMethodAttribute is not optional; any method that is to be exposed as a method of an XML Web Service MUSThave the WebMethodAttribute applied to it, and it MUST be declared using the public keyword. The WebMethodAttribute indicates to the .NET Framework that the specified method should be made accessible via standard Internet protocols.

Since the Web Service method, (a.k.a. Web method), is intended to be accessible using standard Internet protocols, the Web method must be defined as public, indicating that it is accessible from outside the parent class. We can create protectedinternal, or private methods in an XML Web Service class, but they cannot be exposed as Web methods.

The WebMethodAttribute exposes the following properties:

  • BufferResponse - Specified whether the response for this Web method should be buffered. The default is true.
  • CacheDuration - Specifies the number of seconds the Web method response should be stored in cache.
  • Description - Specifies the user-friendly description of the Web method.
  • EnableSession - Specifies whether Session state is enabled for the Web method. The default is false.
  • MessageName - Specifies the name used for the Web method. By default the Web method will be the same name as the method name. Using theMessageName attribute we can use an alias for the Web method. This is particularly useful when building overloaded Web methods – theMessageName attribute allows us to provide different names for the overloaded methods.
  • TransactionOption - Specifies the Enterprise Services transaction support for the Web method.

However, you cannot expose both of them as webmethods because the standard
does not allow more than one function with the same name. You have to
specify different names in the WebMethod attribute:

[WebMethod(MessageName="ModelFamilySearch1")]
public List<ModelFamilyModelFamilySearch(string name)
{
ModelFamilySearch(null, name);
}
[WebMethod(MessageName="ModelFamilySearch2")]
public List<ModelFamilyModelFamilySearch(Nullable<intid, string name)
{

}

Now the problem is that your client code will have to invoke the methods as
ModelFamilySearch1 and ModelFamilySearch2 depending on wether you want to
use one or two arguments. You can fix this by manually modifying the client
proxy to “realias” both calls back into an overloaded method in your client
code. But if you are going to do this, you might as well only expose the
“complete” function as a web method, and then add the overload in the client
code.

Leave a Comment