Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SoapHttpClientProtocol.Discover Method

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)
'Declaration
Public Sub Discover
ExceptionCondition
Exception

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

Exception

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 http://tempuri.org/.

Namespace MyMath

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

        <System.Diagnostics.DebuggerStepThroughAttribute()>  _
        Public Sub New()
            MyBase.New 
            Me.Url = "http://www.contoso.com/math.asmx" 
        End Sub

        <System.Diagnostics.DebuggerStepThroughAttribute(),  _
         System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Add", 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.

Security noteSecurity 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" %>
<html>
    <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

           Try
	     math.Discover()
           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

  </script>

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

          Enter the URL of a disdovery document describing the MathSoap binding.
          <p> 
          <asp:textbox id="DiscoURL" runat=server Columns=80/>
          <p><p>
          Enter the two numbers you want to add and then press the Total button.
          <p>
          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/>
          <p>
          <asp:label id="Total"  runat=server/>

       </form>
    </body>
 </html>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.