使用英语阅读

通过


SoapHttpClientProtocol.BeginInvoke 方法

定义

开始使用 SOAP 异步调用 XML Web services 方法。

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

参数

methodName
String

正调用 BeginInvoke(String, Object[], AsyncCallback, Object) 方法的派生类中的 XML Web services 方法的名称。

parameters
Object[]

对象的数组,包含要传递给 XML Web services 的参数。 数组中值的顺序与派生类的调用方法中的参数顺序对应。

callback
AsyncCallback

异步调用完成时要调用的委托。 如果 callbacknull,则不调用委托。

asyncState
Object

调用方提供的额外信息。

返回

IAsyncResult,传递给 EndInvoke(IAsyncResult) 方法以从远程方法调用中获取返回值。

例外

请求到达了服务器计算机,但未被成功处理。

请求对对象的当前状态无效。

访问网络时出错。

示例

下面的代码示例是由 XML Web 服务的 Web 服务描述语言工具 (Wsdl.exe) Math 生成的代理类。 BeginAdd在代理类的 方法中BeginInvoke, 方法正在启动对 XML Web 服务方法的Add异步调用。


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]));
        }
    }
}

下面的代码示例是 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;
    }
 }

注解

通常,除非要为 XML Web 服务生成自己的代理类,否则不会直接调用 BeginInvoke 方法。

由 Web 服务描述语言工具 (Wsdl.exe) 从服务说明生成的代理类将 XML Web 服务方法公开为派生自代理类的名称,以同步调用 XML Web 服务方法。 为了异步调用 XML Web 服务方法,将另外两个方法添加到每个 XML Web 服务方法的代理类中,一个方法将 Begin 前缀添加到 XML Web 服务方法的名称中,另一个方法 End 添加了 前缀。

代理类调用 BeginInvoke 方法以启动对 XML Web 服务方法的异步调用。 例如,如果 XML Web 服务公开名为 的 AddXML Web 服务方法,则代理类包含一个名为 BeginAdd的方法,用于启动对 XML Web service 方法的调用。 在 的代码 BeginAdd中,对 方法进行 BeginInvoke 调用,并将结果放入 的预期返回类型中 Add

methodName用于查找可能已添加到 方法的自定义属性,例如 SoapDocumentMethodAttributeSoapDocumentMethodAttribute 提供有关 SOAP 协议所需的派生方法的其他信息。

asyncState 传入 callback ,并包含在 IAsyncResultBeginInvoke 方法返回的 中。 参数 asyncState 可用于将有关异步调用上下文的信息(在 参数中指定的 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

另请参阅