SoapHttpClientProtocol.Discover Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Dynamically binds to an XML Web service described in the discovery document at Url.

Namespace:   System.Web.Services.Protocols
Assembly:  System.Web.Services (in System.Web.Services.dll)

Public Sub Discover

Exception Condition

The binding defined in the proxy class could not be found in the discovery document at Url.


The proxy class does not have a binding defined.

An XML Web service client can dynamically bind to an XML Web service other than the one referenced in the proxy class using the Discover method. Typically, the Url property refers to the base address of the XML Web service. However, before invoking the Discover method, set the Url property to a URL of a discovery document. The Discover method attempts to find a match in the discovery document to the binding defined in the proxy class and then dynamically bind to it. If successful, subsequent method calls are directed to the XML Web service described in the discovery document.

If the proxy class is built using the Web Services Description Language tool (Wsdl.exe), the proxy class defines the binding implemented by the XML Web service methods it invokes using the WebServiceBindingAttribute. When an XML Web service implements multiple bindings, Wsdl.exe creates a proxy class for each binding. Applied to each proxy class is a WebServiceBindingAttribute that defines the name of the binding and its namespace. The discovery document you set the Url property to must contain a reference to an XML Web service that implements the same binding name and namespace, or an exception is thrown.

The following code example is a proxy class generated using the Wsdl.exe utility for the Math XML Web service. A WebServiceBindingAttribute is applied to the Math proxy class setting the binding name to MathSoap and its namespace to

Namespace MyMath

    <System.Web.Services.WebServiceBindingAttribute(Name:="MathSoap", [Namespace]:="")>  _
    Public Class Math
        Inherits System.Web.Services.Protocols.SoapHttpClientProtocol

        <System.Diagnostics.DebuggerStepThroughAttribute()>  _
        Public Sub New()
            Me.Url = ""
        End Sub

        <System.Diagnostics.DebuggerStepThroughAttribute(),  _
         System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)>  _
        Public Function Add(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
            Dim results() As Object = Me.Invoke("Add", New Object() {num1, num2})
            Return CType(results(0),Integer)
        End Function

        <System.Diagnostics.DebuggerStepThroughAttribute()>  _
        Public Function BeginAdd(ByVal num1 As Integer, ByVal num2 As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
            Return Me.BeginInvoke("Add", New Object() {num1, num2}, callback, asyncState)
        End Function

        <System.Diagnostics.DebuggerStepThroughAttribute()>  _
        Public Function EndAdd(ByVal asyncResult As System.IAsyncResult) As Integer
            Dim results() As Object = Me.EndInvoke(asyncResult)
            Return CType(results(0),Integer)
        End Function
    End Class
End Namespace

The following code example is a Web Service client using the preceding proxy class. Within the EnterBtn_Click event of the Web Form, the XML Web service client calls the Discover method attempting to dynamically bind to the URL supplied by the user.

System_CAPS_security Security Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="VB" %>
    <script language="VB" runat="server">
    Sub EnterBtn_Click(Src As Object, E As EventArgs)
        Dim math As New Math()
        Dim result As Integer 

        ' If the user types in a URL, attempt to dynamically bind to it.
        If DiscoURL.Text <> String.Empty Then
           math.Url = DiscoURL.Text

           Catch except As Exception
	      DiscoURL.Text = "Could not bind to MathSoap bindng at given URL."
           End Try       

        End If 

        ' Call to Add XML Web service method.
        result = math.Add(Convert.ToInt32(Num1.Text),Convert.ToInt32(Num2.Text))

        Total.Text = "Total: " & result.ToString()
    End Sub 'EnterBtn_Click


       <form action="MathClient.aspx" runat=server>

          Enter the URL of a disdovery document describing the MathSoap binding.
          <asp:textbox id="DiscoURL" runat=server Columns=80/>
          Enter the two numbers you want to add and then press the Total button.
          Number 1: <asp:textbox id="Num1" runat=server/>  +
          Number 2: <asp:textbox id="Num2" runat=server/> =
          <asp:button text="Total" Onclick="EnterBtn_Click" runat=server/>
          <asp:label id="Total"  runat=server/>


.NET Framework
Available since 1.1
Return to top