Sunday, August 12, 2007

Web Service > Consuming

WEB SERVICES: CREATING AND CONSUMING

• Web services should be stateless, they work on similar architecture as the web-page architecture
• Found in System.Web.Services.WebService
• A new web service object is created at the beginning of the request, and is destroyed after processing the request and returning the response
• To expose a web service a tag is required to be put on methods, these methods MUST BE PUBLIC
• Tag looks like: [WebMethod()]
• To make the web service visible, create an ASMX file and link it to the code-behind file containing exposed public web methods

CONSUMING THE WEBSERVICE
• In Visual Studio, add a web reference and select web service (specify URL to wsdl)
• Doing this generates a Proxy Class and is added in the project
• The proxy class has the same name as the web service class
• It inherits from SoapHttpClient Protocol, which has properties such as Credentials, URL, Timeout
• Declaration of Proxy Class looks as under:
public class EmployeeService:System.Web.Services.Protocols.SoapHttpClientProtocol
• The proxy contains a copy of each method in the web service
• However, it PROXY CLASS DOES NOT CONTAIN THE BUSINESS CODE
• Proxy just contains the code needed to query the remote web service and convert the results
• Example of proxy class contents:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute()]
public int GetEmployeesCount()
{
object[] results = this.Invoke(“GetEmployeesCount”, new object[0]);
return ((int)(results[0]));
}

• This method calls the base SoapHttpClientProtocol.Invoke() to actually create the SOAP message and start waiting for the response

No comments:

Post a Comment