HttpSimpleClientProtocol.BeginInvoke 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
启动对 XML Web 服务方法的异步调用。
protected:
IAsyncResult ^ BeginInvoke(System::String ^ methodName, System::String ^ requestUrl, cli::array <System::Object ^> ^ parameters, AsyncCallback ^ callback, System::Object ^ asyncState);
protected IAsyncResult BeginInvoke(string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState);
member this.BeginInvoke : string * string * obj[] * AsyncCallback * obj -> IAsyncResult
Protected Function BeginInvoke (methodName As String, requestUrl As String, parameters As Object(), callback As AsyncCallback, asyncState As Object) As IAsyncResult
参数
- methodName
- String
XML Web 服务方法的名称。
- requestUrl
- String
创建时 WebRequest要使用的 URL。
- parameters
- Object[]
包含要传递给 XML Web 服务方法的参数的对象数组。 数组中值的顺序对应于派生类的调用方法中的参数顺序。
- callback
- AsyncCallback
异步方法调用完成后要调用的委托。
null如果是callback,则不调用委托。
- asyncState
- Object
客户端提供的其他信息。
返回
IAsyncResult可以传递给EndInvoke(IAsyncResult)该方法以获取 XML Web 服务方法的返回值。
例外
请求已到达服务器计算机,但未成功处理。
示例
下面的代码示例是一个 ASP.NET Web 窗体,它调用名为 Math 的 XML Web 服务。 在函数中 EnterBtn_Click ,Web 窗体启动并完成 XML Web 服务方法的 Add 异步调用。
<%@ Page Language="VB" %>
<html>
<script language="VB" runat="server">
Sub EnterBtn_Click(Src As Object, E As EventArgs)
Dim math As New MyMath.Math()
' Call to Add XML Web service method asynchronously.
Dim result As IAsyncResult = math.BeginAdd(Convert.ToInt32(Num1.Text), Convert.ToInt32(Num2.Text), Nothing, Nothing)
' Wait for the asynchronous call to complete.
result.AsyncWaitHandle.WaitOne()
' Complete the asynchronous call to the Add XML Web service method.
Dim iTotal As Integer = math.EndAdd(result)
Total.Text = "Total: " & iTotal.ToString()
End Sub 'EnterBtn_Click
</script>
<body>
<form action="MathClient.aspx" runat=server>
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 服务的 Web 服务描述语言工具(Wsdl.exe) Math 生成的代理类。 在 BeginAdd 代理类的方法中,该方法 BeginInvoke 启动 XML Web 服务方法的 Add 异步调用。
namespace MyMath
{
[XmlRootAttribute("snippet1>",Namespace="http://MyMath/",IsNullable=false)]
public ref class Math: public HttpGetClientProtocol
{
public:
Math()
{
this->Url = "http://www.contoso.com/math.asmx";
}
[HttpMethodAttribute(System::Web::Services::Protocols::XmlReturnReader::typeid,
System::Web::Services::Protocols::UrlParameterWriter::typeid)]
int Add( String^ num1, String^ num2 )
{
array<Object^>^temp0 = {num1,num2};
return *dynamic_cast<int^>(this->Invoke( "Add", String::Concat( this->Url, "/Add" ), temp0 ));
}
IAsyncResult^ BeginAdd( String^ num1, String^ num2, AsyncCallback^ callback, Object^ asyncState )
{
array<Object^>^temp1 = {num1,num2};
return this->BeginInvoke( "Add", String::Concat( this->Url, "/Add" ), temp1, callback, asyncState );
}
int EndAdd( IAsyncResult^ asyncResult )
{
return *dynamic_cast<int^>(this->EndInvoke( asyncResult ));
}
};
}
namespace MyMath
{
[XmlRootAttribute("int", Namespace = "http://MyMath/", IsNullable = false)]
public class Math : HttpGetClientProtocol
{
public Math()
{
this.Url = "http://www.contoso.com/math.asmx";
}
[HttpMethodAttribute(typeof(System.Web.Services.Protocols.XmlReturnReader),
typeof(System.Web.Services.Protocols.UrlParameterWriter))]
public int Add(int num1, int num2)
{
return ((int)(this.Invoke("Add", ((this.Url) + ("/Add")),
new object[] { num1, num2 })));
}
public IAsyncResult BeginAdd(int num1, int num2, AsyncCallback callback, object asyncState)
{
return this.BeginInvoke("Add", ((this.Url) + ("/Add")),
new object[] { num1, num2 }, callback, asyncState);
}
public int EndAdd(IAsyncResult asyncResult)
{
return ((int)(this.EndInvoke(asyncResult)));
}
}
}
Namespace MyMath
<XmlRootAttribute("int", Namespace := "http://MyMath/", IsNullable := False)> _
Public Class Math
Inherits HttpGetClientProtocol
Public Sub New()
Me.Url = "http://www.contoso.com/math.asmx"
End Sub
<HttpMethodAttribute(GetType(XmlReturnReader), GetType(UrlParameterWriter))> _
Public Function Add(num1 As String, num2 As String) As Integer
Return CInt(Me.Invoke("Add", Me.Url + "/Add", New Object() {num1, num2}))
End Function 'Add
Public Function BeginAdd(num1 As String, num2 As String, callback As AsyncCallback, asyncState As Object) As IAsyncResult
Return Me.BeginInvoke("Add", Me.Url + "/Add", New Object() {num1, num2}, callback, asyncState)
End Function 'BeginAdd
Public Function EndAdd(asyncResult As IAsyncResult) As Integer
Return CInt(Me.EndInvoke(asyncResult))
End Function 'EndAdd
End Class
End Namespace 'MyMath
下面的代码示例是 Math 创建上述代理类的 XML Web 服务。
<%@ WebService Language="C#" Class="Math"%>
using System.Web.Services;
using System;
public class Math {
[ WebMethod ]
public int Add(int num1, int num2) {
return num1+num2;
}
}
<%@ WebService Language="VB" Class="Math"%>
Imports System.Web.Services
Imports System
Public Class Math
<WebMethod()> _
Public Function Add(num1 As Integer, num2 As Integer) As Integer
Return num1 + num2
End Function 'Add
End Class 'Math
注解
该 methodName 参数用于查找参数的类型,并返回调用该方法的方法 BeginInvoke 的值。 它还用于查找可能已添加到方法的自定义属性。
SoapDocumentMethodAttribute, SoapRpcMethodAttribute并提供 XmlElementAttribute 有关 HTTP 协议所需的派生方法的其他信息。
asyncState 传入 callback 并包含在 IAsyncResult 从方法返回的 BeginInvoke 中。 它可用于将异步调用上下文中的信息传递给异步结果 callback的处理。