简体   繁体   English

带有Silverlight异步混淆的ASMX Web服务

[英]ASMX webservices with Silverlight Async confusion

I have a silverlight 4 web app that needs to communicate with a server by accessing the ASMX web service on the server. 我有一个Silverlight 4 Web应用程序,该应用程序需要通过访问服务器上的ASMX Web服务来与服务器进行通信。 I have a list(yes, the array), of objects that I need to send(one by one) as a parameter to the service. 我有一个列表(是的,数组),我需要将对象(一个一个地发送)作为服务的参数。 However looping through the list and running the method(objecttosend); 但是循环遍历列表并运行方法(objecttosend); will not work because I need to send then one after another and Silverlight seems to only support Async(presumably to not lockup interface - makes sense). 将无法正常工作,因为我需要一个接一个地发送,而Silverlight似乎仅支持Async(大概不支持锁定接口-很有道理)。

So I tried this: 所以我尝试了这个:

public void SendNextPart()
    {
        if (partsToSend.Count > 0)
        {
            Part thisPart = partsToSend.Dequeue();
            fuWS.createPartCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(fuWS_createPartCompleted);
            fuWS.createPartAsync(thisPart);

        }
    }
Queue<Part> partsToSend = new Queue<Part>();
    void fuWS_createPartCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SendNextPart();
        }

Which, as far as I can understand it, will check to see if the List has parts to send, then run the webservice(called fuWS) method and delete that part from the partsToSend List. 据我所知,它将检查列表是否有要发送的部分,然后运行webservice(称为fuWS)方法并从partsToSend列表中删除该部分。 Once it gets the completed event it should then run the SendNextPart method again and send the next part. 一旦获得完成事件,则应再次运行SendNextPart方法并发送下一部分。

However what is happening(picked this up by watching HTTPwatch) is that it sends the first part, then after that is sends 2 parts at once and then after that more and more, all at once. 但是发生了什么(通过观看HTTPwatch对此进行了记录)是它发送了第一部分,然后又一次发送了2个部分,然后又发送了越来越多的所有部分。 Almost as if it is receiving the completed event before it has actually sent to the server and run the method successfully. 几乎就好像它在实际发送到服务器并成功运行该方法之前正在接收已完成的事件。

Please help, this is bugging the hell out of me, and it completely breaks what I need to do :'( 请帮助,这使我烦恼不已,这完全破坏了我需要做的事情:'(

I don't see the SendNextBuffer method that you're calling in the web service callback event handler. 我没有在Web服务回调事件处理程序中看到正在调用的SendNextBuffer方法。 But in any case, at best your code has a race condition. 但无论如何,充其量您的代码具有竞争条件。 If the web service completes and returns before the partsToSend.RemoveAt line is executed (theoretically possible) then you could be making the next request before you've removed the one you just sent. 如果Web服务完成并在执行partsToSend.RemoveAt行之前返回(从理论上讲是可能的),那么您可能要发出下一个请求,然后再删除刚发送的请求。

So first, you should check to make sure you've included all the code in your example unless you meant for SendNextBuffer to say SendNextPart . 因此,首先,您应该检查并确保示例中包含了所有代码,除非您SendNextBuffer说出SendNextPart

Secondly, you should move the partsToSend.RemoveAt line before the web service call. 其次,您应该调用Web服务之前partsToSend.RemoveAt行。

Finally, you should probably change the partsToSend list into a Queue<Part> (first in, first out) or Stack<Part> (last in, first out) instead since that is what you're using it as. 最后,您可能应该将partsToSend列表更改为Queue<Part> (先进先出)或Stack<Part> (后进先出),因为这就是您使用它的方式。

Ok, so after using Debug.WriteLine, I realized that I was being an idiot. 好的,所以在使用Debug.WriteLine之后,我意识到自己是个白痴。 Check out this line: 查看以下行:

fuWS.createPartCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(fuWS_createPartCompleted);

What this was doing was adding a new event handler every time it had to send a new part. 这样做是在每次必须发送新零件时添加一个新的事件处理程序。 So the second part sending now had two callback then the third would have more and so on increasing exponentially. 因此,第二部分发送现在有两个回调,而第三部分将有更多,以此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM