调用C#ASMX Web服务

时间:2012-11-11 15:47:32

标签: c# asmx iasyncresult waithandle

我有一个ASMX Web服务,我需要将其用作一项工作的一部分。我通过ASPX页面调用此服务来在第三方系统上创建新实体。我无法访问该服务的底层代码,只是为了让我与另一个系统进行通信。

我很难发现我是否正确地调用了该服务,我想知道是否有人可以提供一些建议。

我已经安装了ASMX页面并且给了我一个类'ConfirmConnector',我称之为BeginProcessOperations方法。我想等待返回,然后解析结果。结果应该是XML格式,然后我会逐步获取我想要的数据。

麻烦的是,有时这个过程就在我身上,即当我调用我的'EndProcessOperations'方法时,没有任何反应。我没有得到错误,没有 - 我的代码只是死了,方法返回'

我的主叫代码是:

private void sendConfirmRequest(XmlManipulator requestXML)
{
    file.WriteLine("Sending CONFIRM Request!");
    AsyncCallback callBack = new AsyncCallback(processConfirmXML); // assign the callback method for this call

    IAsyncResult r = conn.BeginProcessOperations(requestXML, callBack, AsyncState);
    System.Threading.WaitHandle[] waitHandle = { r.AsyncWaitHandle }; // set up a wait handle so that the process doesnt automatically return to the ASPX page
    System.Threading.WaitHandle.WaitAll(waitHandle, -1);
}

我的处理程序代码是:

 /*
 * Process the response XML from the CONFIRM Connector
 */
private static void processConfirmXML(IAsyncResult result)
{
    try
    {
        file.WriteLine("Received Response from CONFIRM!");
        if(result == null)
        {
            file.WriteLine("RESPONSE is null!!");
        }
        if(conn == null)
        {
            file.WriteLine("conn is null!!");
        }
        file.WriteLine("Is Completed : " + result.IsCompleted);

        XmlNode root =  conn.EndProcessOperations(result);
        file.WriteLine("got return XML");
        //writeXMLToFile("C:/response.xml",root.InnerXml);
        file.WriteLine(root.InnerXml);

任何人都可以建议我是否以正确的方式处理此代码,并且有人知道为什么我的代码会在处理程序中的这一行之后随机炸弹:

XmlNode root =  conn.EndProcessOperations(result);

感谢您的帮助, 保罗

1 个答案:

答案 0 :(得分:0)

谢谢你的期待,但我解决了我的问题。该问题似乎与我的回调操作有关。

我更改了代码以调用我的开头和&在同一代码块中结束方法,从那时起我就没有问题了。

private void sendConfirmRequest(XmlManipulator requestXML)
{
    //ConfirmConnector conn = new ConfirmConnector();
    file.WriteLine("Sending CONFIRM Request!");
    //AsyncCallback callBack = new AsyncCallback(processConfirmXML); // assign the callback method for this call

    //IAsyncResult r = conn.BeginProcessOperations(requestXML, callBack, AsyncState);
    //System.Threading.WaitHandle[] waitHandle = { r.AsyncWaitHandle }; // set up a wait handle so that the process doesnt automatically return to the ASPX page
    //System.Threading.WaitHandle.WaitAll(waitHandle, -1);

    file.WriteLine("Calling BeginProcessOperations");
    IAsyncResult result = conn.BeginProcessOperations(requestXML, null, null);
    // Wait for the WaitHandle to become signaled.
    result.AsyncWaitHandle.WaitOne();
    file.WriteLine("Calling EndProcessOperations");
    XmlNode root = conn.EndProcessOperations(result);
    processConfirmXML(root);

    file.WriteLine("got return XML");
    //writeXMLToFile("C:/response.xml",root.InnerXml);
    file.WriteLine(root.InnerXml);

    // Close the wait handle.
    result.AsyncWaitHandle.Close();
}

由于