HttpSimpleClientProtocol.BeginInvoke 方法

定義

啟動 XML Web Service 方法的非同步引動過程。

C#
protected IAsyncResult BeginInvoke(string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState);

參數

methodName
String

XML Web Service 方法的名稱。

requestUrl
String

建立 WebRequest 時所使用的 URL。

parameters
Object[]

物件的陣列,包含要傳遞至 XML Web Service 方法的參數。 陣列中值的順序會對應至衍生類別之呼叫方法中參數的順序。

callback
AsyncCallback

非同步方法呼叫完成後,要呼叫的委派。 如果 callbacknull,則不呼叫委派。

asyncState
Object

用戶端提供的額外資訊。

傳回

IAsyncResult,可傳遞至 EndInvoke(IAsyncResult) 方法,以從 XML Web Service 方法取得傳回值。

例外狀況

要求已到達伺服器電腦,但是並未成功處理。

範例

下列程式碼範例是 ASP.NET Web Form,它會呼叫名為 的 Math XML Web 服務。 在 函 EnterBtn_Click 式中,Web Form 會啟動並完成 XML Web 服務方法的 Add 非同步調用。

ASP.NET (C#)

下列程式碼範例是由下列 XML Web 服務的 Web 服務描述語言工具 (Wsdl.exe) Math 所產生的 Proxy 類別。 在 BeginAdd Proxy 類別的 方法內 BeginInvoke ,方法會啟動 XML Web 服務方法的 Add 非同步調用。

C#
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)));
        }
    }
}

下列程式碼範例是 Math 建立上述 Proxy 類別的 XML Web 服務。

ASP.NET (C#)
<%@ 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;
          }
 }

備註

參數 methodName 是用來尋找參數的類型,並傳回叫用 BeginInvoke 方法的方法值。 它也可用來尋找可能已新增至 方法的自訂屬性。 SoapDocumentMethodAttributeSoapRpcMethodAttributeXmlElementAttribute 提供 HTTP 通訊協定所需衍生方法的其他資訊。

asyncStatecallback 傳入 ,並包含在 IAsyncResult 方法傳回的 中 BeginInvoke 。 它適用于將非同步呼叫內容中的資訊傳遞至 中 callback 非同步結果的處理。

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另請參閱