共用方式為


SoapHttpClientProtocol.Discover 方法

定義

動態繫結至 Url 的探索文件中所述的 XML Web Service。

public:
 void Discover();
public void Discover ();
member this.Discover : unit -> unit
Public Sub Discover ()

例外狀況

無法在 Url 的探索文件中找到 Proxy 類別中定義的繫結。

-或-

Proxy 類別不會定義繫結。

範例

下列程式代碼範例是使用 XML Web 服務的 Wsdl.exe 公用程式 Math 所產生的 Proxy 類別。 WebServiceBindingAttribute會套用至 Proxy 類別,Math將系結名稱設定為 MathSoap ,並將其命名空間設定為 http://tempuri.org/

#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="MathSoap",Namespace="http://tempuri.org/")]
   public ref class Math: public System::Web::Services::Protocols::SoapHttpClientProtocol
   {
   public:

      [System::Diagnostics::DebuggerStepThroughAttribute]
      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)]
      int Add( int num1, int num2 )
      {
         array<Object^>^temp0 = {num1,num2};
         array<Object^>^results = this->Invoke( "Add", temp0 );
         return  *dynamic_cast<int^>(results[ 0 ]);
      }


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


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

   };

}

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]));
        }
    }
}
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

下列程式代碼範例是使用上述 Proxy 類別的 Web 服務用戶端。 EnterBtn_Click在 Web Form 的事件中,XML Web 服務用戶端會呼叫Discover嘗試動態系結至使用者所提供的 URL 的方法。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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>
<%@ 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>

備註

XML Web 服務用戶端可以動態系結至 XML Web 服務,而不是使用 Discover 方法在 Proxy 類別中參考的 XML Web 服務。 Url屬性通常是指 XML Web 服務的基位址。 不過,在叫用 Discover 方法之前,請將 Url 屬性設定為探索檔的URL。 方法 Discover 會嘗試在探索檔中尋找相符專案與 Proxy 類別中定義的系結,然後動態系結至它。 如果成功,後續的方法呼叫會導向至探索檔中所述的 XML Web 服務。

如果使用 Web 服務描述語言工具建置 Proxy 類別 (Wsdl.exe) ,Proxy 類別會定義它使用 叫用 WebServiceBindingAttribute的 XML Web 服務方法所實作的系結。 當 XML Web 服務實作多個系結時,Wsdl.exe 為每個系結建立 Proxy 類別。 套用至每個 Proxy 類別的 , WebServiceBindingAttribute 定義系結的名稱及其命名空間。 您設定 屬性的 Url 探索檔必須包含實作相同系結名稱和命名空間的 XML Web 服務參考,否則會擲回例外狀況。

適用於

另請參閱