发表时间:2007-7-3 14:41:00

WebService方法代码:
[WebMethod]
public string HelloWorld() {
    System.Threading.Thread.Sleep(3000);
    return "Hello World";
}

调用wsdl ****.asmx /o:d:"ServiceA.cs
生成WebService代理类,并加入工程

调用页代码:
public partial class _Default : System.Web.UI.Page
{
    global::_ServiceA serviceA = new _ServiceA();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e) {
        this.TextBox1.Text = "";
       
        AsyncCallback asyncCallback = new AsyncCallback(CallBackExec);

        IAsyncResult asyncResult = serviceA.BeginHelloWorld(asyncCallback, null);
        asyncResult.AsyncWaitHandle.WaitOne();

        /*
        也可不用回调函数asyncCallBack
        string result = "";
        if (asyncResult.IsCompleted) {
            result = serviceA.EndHelloWorld(asyncResult);
        }
        this.TextBox1.Text = result;*/
    }

    private void CallBackExec(IAsyncResult asyncResult) {

        Response.Write("CallBackExec() called!");
        Response.Write(asyncResult.IsCompleted);

        this.TextBox1.Text = serviceA.EndHelloWorld(asyncResult);
    }
}

posted on 2009-04-29 15:17  袁晓平  阅读(251)  评论(0编辑  收藏  举报