SoapHttpClientProtocol.Discover 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
动态绑定到 Url 处的发现文档中描述的 XML Web services。
public:
void Discover();
public void Discover ();
member this.Discover : unit -> unit
Public Sub Discover ()
例外
示例
下面的代码示例是使用 XML Web 服务的 Wsdl.exe 实用工具 Math
生成的代理类。
WebServiceBindingAttribute将 应用于将绑定名称MathSoap
设置为 的Math
代理类,并将其命名空间设置为 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
下面的代码示例是使用上述代理类的 Web 服务客户端。 在 EnterBtn_Click
Web 窗体的 事件中,XML Web 服务客户端调用 Discover 方法,尝试动态绑定到用户提供的 URL。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ 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 服务客户端可以使用 方法动态绑定到代理类 Discover 中引用的服务以外的 XML Web 服务。 通常, Url 属性引用 XML Web 服务的基址。 但是,在调用 Discover 方法之前,请将 Url 属性设置为发现文档的 URL。 方法 Discover 尝试在发现文档中查找与代理类中定义的绑定的匹配项,然后动态绑定到它。 如果成功,后续方法调用将定向到发现文档中所述的 XML Web 服务。
如果代理类是使用 Web 服务描述语言工具 (Wsdl.exe) 生成的,则代理类定义由它使用 调用的 XML Web 服务方法实现的 WebServiceBindingAttribute绑定。 当 XML Web 服务实现多个绑定时,Wsdl.exe 为每个绑定创建一个代理类。 应用于每个代理类的是一个 WebServiceBindingAttribute ,用于定义绑定的名称及其命名空间。 将 属性设置为 Url 的发现文档必须包含对实现相同绑定名称和命名空间的 XML Web 服务的引用,否则将引发异常。