SoapHttpClientProtocol.BeginInvoke 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
開始使用 SOAP 非同步呼叫 XML Web 服務方法。
protected:
IAsyncResult ^ BeginInvoke(System::String ^ methodName, cli::array <System::Object ^> ^ parameters, AsyncCallback ^ callback, System::Object ^ asyncState);
protected IAsyncResult BeginInvoke(string methodName, object[] parameters, AsyncCallback callback, object asyncState);
member this.BeginInvoke : string * obj[] * AsyncCallback * obj -> IAsyncResult
Protected Function BeginInvoke (methodName As String, parameters As Object(), callback As AsyncCallback, asyncState As Object) As IAsyncResult
參數
- methodName
- String
呼叫該方法的衍生類別 BeginInvoke(String, Object[], AsyncCallback, Object) 中 XML Web 服務方法的名稱。
- parameters
- Object[]
一個包含要傳遞給 XML Web 服務參數的物件陣列。 陣列中值的順序對應於導出類別呼叫方法中參數的順序。
- callback
- AsyncCallback
當非同步調用完成時,代表將呼叫。 若 callback , null則不叫代表。
- asyncState
- Object
來電者提供的額外資訊。
傳回
IAsyncResult會將 一個 傳遞給EndInvoke(IAsyncResult)方法,以取得遠端方法呼叫的回傳值。
例外狀況
請求已抵達伺服器電腦,但未被成功處理。
該請求對物件目前狀態無效。
在存取網路時發生錯誤。
範例
以下程式碼範例是由 Web Services Description Language 工具(Wsdl.exeMath )為 XML Web 服務產生的代理類別。 在代理類別的方法中 BeginAdd ,該 BeginInvoke 方法開始對 XML Web 服務方法進行非同步調用 Add 。
#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="MyMathSoap",Namespace="http://www.contoso.com/")]
public ref class MyMath: public System::Web::Services::Protocols::SoapHttpClientProtocol
{
public:
[System::Diagnostics::DebuggerStepThroughAttribute]
MyMath()
{
this->Url = "http://www.contoso.com/math.asmx";
}
[System::Diagnostics::DebuggerStepThroughAttribute]
[System::Web::Services::Protocols::SoapDocumentMethodAttribute("http://www.contoso.com/Add",
RequestNamespace="http://www.contoso.com/",ResponseNamespace="http://www.contoso.com/",
Use=System::Web::Services::Description::SoapBindingUse::Literal,
ParameterStyle=System::Web::Services::Protocols::SoapParameterStyle::Wrapped)]
int Add( int num1, int num2 )
{
array<Object^>^temp1 = {num1,num2};
array<Object^>^results = this->Invoke( "Add", temp1 );
return *dynamic_cast<int^>(results[ 0 ]);
}
[System::Diagnostics::DebuggerStepThroughAttribute]
System::IAsyncResult^ BeginAdd( int num1, int num2, System::AsyncCallback^ callback, Object^ asyncState )
{
array<Object^>^temp2 = {num1,num2};
return this->BeginInvoke( "Add", temp2, 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="MyMathSoap", Namespace="http://www.contoso.com/")]
public class MyMath : System.Web.Services.Protocols.SoapHttpClientProtocol {
[System.Diagnostics.DebuggerStepThroughAttribute()]
public MyMath() {
this.Url = "http://www.contoso.com/math.asmx";
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.contoso.com/Add", RequestNamespace="http://www.contoso.com/", ResponseNamespace="http://www.contoso.com/", 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]));
}
}
}
Option Strict On
Option Explicit On
Imports System.Diagnostics
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Serialization
Namespace MyMath
<System.Web.Services.WebServiceBindingAttribute(Name:="MyMathSoap", [Namespace]:="http://www.contoso.com/")> _
Public Class MyMath
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://www.contoso.com/Add", RequestNamespace:="http://www.contoso.com/", ResponseNamespace:="http://www.contoso.com/", 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
以下程式碼範例是 Math XML Web 服務,前述代理類別即由此產生。
<%@ WebService Language="C#" Class="MyMath"%>
using System.Web.Services;
using System;
[WebService(Namespace="http://www.contoso.com/")]
public class MyMath {
[ WebMethod ]
public int Add(int num1, int num2) {
return num1+num2;
}
}
<%@ WebService Language="VB" Class="MyMath"%>
Imports System.Web.Services
Imports System
<WebService(Namespace:="http://www.contoso.com/")> _
Public Class MyMath
<WebMethod()> _
Public Function Add(num1 As Integer, num2 As Integer) As Integer
Return num1 + num2
End Function 'Add
End Class 'Math
備註
通常,除非你正在為 XML Web 服務建置代理類別,否則你不會直接呼叫這個 BeginInvoke 方法。
由 Web Services Description Language 工具(Wsdl.exe)從服務描述產生的代理類別,會以代理類別衍生的名稱公開 XML Web 服務方法,以同步呼叫 XML Web 服務方法。 要非同步呼叫 XML Web 服務方法,每個 XML Web 服務方法的代理類別中會新增兩個方法,一個在 Begin XML Web 服務方法名稱後加上前綴,另一個則 End 加上前綴。
代理類別會呼叫該 BeginInvoke 方法,以啟動對 XML Web 服務方法的非同步調用呼叫。 例如,若 XML Web 服務暴露了一個名為 Add的 XML Web 服務方法,該代理類別包含一個名為 BeginAdd的方法,用於啟動對 XML Web 服務方法的呼叫。 在 的 BeginAdd程式碼中,會 BeginInvoke 呼叫該方法,並將結果放入預期的回傳類型 Add。
用來 methodName 尋找可能被加入方法的自訂屬性,例如 SoapDocumentMethodAttribute。
SoapDocumentMethodAttribute 提供關於 SOAP 協議所需導出方法的額外資訊。
asyncState 傳遞到 callback 並包含在 IAsyncResult 從方法回 BeginInvoke 傳的 中。
asyncState參數可用來將參數中callback指定的非同步呼叫上下文資訊傳遞給處理結果的代理。