Two articles How to create the proxy class with wsdl command-line tool in ASP.NET and How to create the proxy class with Visual Studio in ASP.NET describe as simple tasks how you can generate a proxy class used to consume a web service. This article gives more information about how the proxy class works.
The proxy class inherits from SoapHttpClientProtocol and it has the same name as the web service class. The declaration of the proxy class is look like:
In C#
public class Books :
System.Web.Services.Protocols.SoapHttpClientProtocol
{ … }
In VB.NET
Public Class Books
Inherits System.Web.Services.Protocols.SoapHttpClientProtocol
End Class
The proxy class contains a copy of each method in the web service, but the version in the class does not contain the business code. The proxy class contains the code needed to query the remote web service and convert the results. The next code lines show this:
In C#
[System.Web.Services.Protocols.SoapDocumentMethodAttribute()]
public int GetBooksCount()
{
object[] results = this.Invoke(“GetBooksCount”, new object[0]);
return ((int)(results[0]));
}
In VB.NET
<System.Web.Services.Protocols.SoapDocumentMethodAttribute()> _
Public Function GetBooksCount() As Integer
Dim Results As Object() = Me.Invoke(“GetBooksCount”, New Object(0) {})
Return CInt(Results(0))
End Function
This method calls the base SoapHttpClientProcotol.Invoke() to actually create the SOAP message and start waiting for the response. The second line of code converts the returned object into an integer.
In case when the web service returns a set of data, the proxy class’s method returns a value which is converted to a DataSet instead than an integer:
In C#
[System.Web.Services.Protocols.SoapDocumentMethodAttribute()]
public System.Data.DataSet GetBooks()
{
object[] results = this.Invoke(“GetBooks”, new object[0]);
return ((System.Data.DataSet)(results[0]));
}
In VB.NET
<System.Web.Services.Protocols.SoapDocumentMethodAttribute()> _
Public Function GetBooks() As System.Data.DataSet
Dim Results As Object() = Me.Invoke(“GetBooks”, New Object(0) {})
Return DirectCast(Results(0), System.Data.DataSet)
End Function