HttpSimpleClientProtocol.BeginInvoke(String, String, Object[], AsyncCallback, Object) 方法

定义

开始异步调用 XML Web services 的方法。Starts an asynchronous invocation of a method of an XML Web service.

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 services 方法的名称。The name of the XML Web service method.

requestUrl
String

创建 WebRequest 时要使用的 URL。The URL to use when creating the WebRequest.

parameters
Object[]

一个对象数组,包含要传递到 XML Web services 方法的参数。An array of objects containing the parameters to pass to the XML Web service method. 数组中值的顺序与派生类的调用方法中的参数顺序对应。The order of the values in the array corresponds to the order of the parameters in the calling method of the derived class.

callback
AsyncCallback

异步方法调用完成时要调用的委托。The delegate to call when the asynchronous method call is complete. 如果 callbacknull,则不调用委托。If callback is null, the delegate is not called.

asyncState
Object

客户端提供的其他信息。The additional information supplied by a client.

返回

IAsyncResult

一个 IAsyncResult,它可以传递到 EndInvoke(IAsyncResult) 方法以从 XML Web services 方法中获取返回值。An IAsyncResult that can be passed to the EndInvoke(IAsyncResult) method to obtain the return values from the XML Web service method.

例外

请求到达了服务器计算机,但未被成功处理。The request reached the server computer, but was not processed successfully.

示例

下面的代码示例是 ASP.NET Web 窗体,它调用名为的 XML Web service MathThe following code example is an ASP.NET Web Form, which calls an XML Web service named Math. EnterBtn_Click 函数中,Web 窗体启动并完成 XML Web service 方法的异步调用 AddWithin the EnterBtn_Click function, the Web Form starts and completes an asynchronous invocation of the Add XML Web service method.

<%@ Page Language="C#" %>
<html>
    <script language="C#" runat="server">
       void EnterBtn_Click(Object Src, EventArgs E) 
          {
             MyMath.Math math = new MyMath.Math();
 
         // Call the Add XML Web service method asynchronously.
         IAsyncResult result = math.BeginAdd(Convert.ToInt32(Num1.Text), Convert.ToInt32(Num2.Text), null, null);
             
         // Wait for the asynchronous call to complete.
         result.AsyncWaitHandle.WaitOne();
 
         // Complete the asynchronous call to the Add XML Web service method.
         int total = math.EndAdd(result);
 
         Total.Text = "Total: " + total.ToString();
         }
 
    </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>
<%@ 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>

下面的代码示例是一个由 Web 服务描述语言工具生成的代理类, ( # A0) 的 Math XML Web service 如下所示。The following code example is a proxy class generated by the Web Services Description Language tool (Wsdl.exe) for the Math XML Web service below. BeginAdd proxy 类的方法中, BeginInvoke 方法启动 XML Web service 方法的异步调用 AddWithin the BeginAdd method of the proxy class, the BeginInvoke method starts an asynchronous invocation of the Add XML Web service method.

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 service。The following code example is the Math XML Web service, from which the preceding proxy class was created.

<%@ 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参数用于查找调用方法的方法的参数类型和返回值 BeginInvokeThe methodName parameter is used to find the types of the parameters and return values of the method that is invoking the BeginInvoke method. 它还用于查找可能已添加到方法中的自定义属性。It is also used to find custom attributes that may have been added to the method. SoapDocumentMethodAttributeSoapRpcMethodAttributeXmlElementAttribute 提供有关 HTTP 协议所需的派生方法的其他信息。SoapDocumentMethodAttribute, SoapRpcMethodAttribute, and XmlElementAttribute provide additional information on the derived method that is required for the HTTP protocol.

asyncState 传入 callback ,并且包含在 IAsyncResult 从方法返回的中 BeginInvokeasyncState is passed into callback and is included in the IAsyncResult that is returned from the BeginInvoke method. 此方法可用于将异步调用上下文中的信息传递给中的异步结果处理 callbackIt is useful for passing information from the context of the asynchronous call to the handling of the asynchronous result in callback.

适用于

另请参阅