Export (0) Print
Expand All

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)

public void 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 {
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System;
    using System.Web.Services.Protocols;
    using System.Web.Services;


    [System.Web.Services.WebServiceBindingAttribute(Name="MathSoap", Namespace="http://tempuri.org/")]
    public class Math : System.Web.Services.Protocols.SoapHttpClientProtocol {

        [System.Diagnostics.DebuggerStepThroughAttribute()]
        public Math() {
            this.Url = "http://www.contoso.com/math.asmx";
        }

        [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 int Add(int num1, int num2) {
            object[] results = this.Invoke("Add", new object[] {num1,
                        num2});
            return ((int)(results[0]));
        }

        [System.Diagnostics.DebuggerStepThroughAttribute()]
        public System.IAsyncResult BeginAdd(int num1, int num2, System.AsyncCallback callback, object asyncState) {
            return this.BeginInvoke("Add", new object[] {num1,
                        num2}, callback, asyncState);
        }

        [System.Diagnostics.DebuggerStepThroughAttribute()]
        public int EndAdd(System.IAsyncResult asyncResult) {
            object[] results = this.EndInvoke(asyncResult);
            return ((int)(results[0]));
        }
    }
}
#using <mscorlib.dll>
#using <System.Web.Services.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System::Diagnostics;
using namespace System::Xml::Serialization;
using namespace System;
using namespace System::Web::Services::Protocols;
using namespace System::Web::Services;

namespace MyMath {

   [System::Web::Services::WebServiceBindingAttribute(Name=S"MathSoap", Namespace=S"http://tempuri.org/")]
   public __gc class Math : public System::Web::Services::Protocols::SoapHttpClientProtocol {

   public:
      [System::Diagnostics::DebuggerStepThroughAttribute]
      Math() {
         this->Url = S"http://www.contoso.com/math.asmx";
      }

      [System::Diagnostics::DebuggerStepThroughAttribute]
      [System::Web::Services::Protocols::SoapDocumentMethodAttribute(S"http://tempuri.org/Add",
         Use=System::Web::Services::Description::SoapBindingUse::Literal,
         ParameterStyle=System::Web::Services::Protocols::SoapParameterStyle::Wrapped)]
      int Add(int num1, int num2) {
         Object* temp0 [] = {__box(num1), __box(num2)};
         Object* results[] = this->Invoke(S"Add", temp0);
         return *dynamic_cast<__box int*>(results[0]);
      }

      [System::Diagnostics::DebuggerStepThroughAttribute]
      System::IAsyncResult* BeginAdd(int num1, int num2, System::AsyncCallback* callback, Object* asyncState) {
         Object* temp1 [] = {__box(num1), __box(num2)};
         return this->BeginInvoke(S"Add", temp1, callback, asyncState);
      }

      [System::Diagnostics::DebuggerStepThroughAttribute]
      int EndAdd(System::IAsyncResult* asyncResult) {
         Object* results[] = this->EndInvoke(asyncResult);
         return *dynamic_cast<__box int*>(results[0]);
      }
   };
}

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="C#" %>
<html>
    <script language="C#" runat="server">
       void EnterBtn_Click(Object Src, EventArgs E) 
          {
             MyMath.Math math = new MyMath.Math();
             // If the user types in a URL, attempt to dynamically bind to it. 
             if (DiscoURL.Text != String.Empty)
                { 
                  math.Url = DiscoURL.Text;
                  try
                      { math.Discover();}
                  catch (Exception)
                      {
                        DiscoURL.Text = "Could not bind to MathSoap bindng at given URL.  ";
                      }
                }

         // Call the Add XML Web service method.  
         int total = math.Add(Convert.ToInt32(Num1.Text),Convert.ToInt32(Num2.Text));

             Total.Text = "Total: " + total.ToString();
         }

    </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>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft