ASMX Web 服务与 Silverlight 异步混淆

发布于 2024-09-10 17:01:36 字数 1039 浏览 1 评论 0原文

我有一个 silverlight 4 Web 应用程序,需要通过访问服务器上的 ASMX Web 服务来与服务器进行通信。 我有一个对象列表(是的,数组),我需要将其作为参数发送(一个接一个)到服务。然而,循环遍历列表并运行方法(objecttosend);不会工作,因为我需要一个接一个地发送,而Silverlight似乎只支持异步(大概不锁定接口 - 有意义)。

所以我尝试了这个:

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

据我所知,它将检查列表是否有要发送的部分,然后运行 ​​webservice(称为 fuWS)方法并从 partsToSend 列表中删除该部分。一旦获得完成的事件,它应该再次运行 SendNextPart 方法并发送下一部分。

然而正在发生的事情(通过观看 HTTPwatch 发现这一点)是它发送第一部分,然后之后一次发送 2 部分,然后越来越多,一次全部发送。几乎就像它在实际发送到服务器并成功运行该方法之前接收已完成的事件一样。

请帮忙,这让我很烦恼,它完全破坏了我需要做的事情:'(

I have a silverlight 4 web app that needs to communicate with a server by accessing the ASMX web service on the server.
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); 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).

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. Once it gets the completed event it should then run the SendNextPart method again and send the next part.

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. 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 :'(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭⌒浅淡时光〆 2024-09-17 17:01:36

我没有看到您在 Web 服务回调事件处理程序中调用的 SendNextBuffer 方法。但无论如何,您的代码至多有竞争条件。如果 Web 服务在执行 partsToSend.RemoveAt 行之前完成并返回(理论上是可能的),那么您可能会在删除刚刚发送的请求之前发出下一个请求。

因此,首先,您应该检查以确保您已在示例中包含所有代码,除非您想让 SendNextBuffer 表示 SendNextPart

其次,您应该将 partsToSend.RemoveAt 行移至 Web 服务调用之前。

最后,您可能应该将 partsToSend 列表更改为 Queue (先进先出)或 Stack (后进先出),因为这就是你所使用的。

I don't see the SendNextBuffer method that you're calling in the web service callback event handler. 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.

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.

Secondly, you should move the partsToSend.RemoveAt line before the web service call.

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.

眉目亦如画i 2024-09-17 17:01:36

好吧,在使用 Debug.WriteLine 之后,我意识到我是个白痴。
查看这一行:

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

它所做的是每次必须发送新部分时添加一个新的事件处理程序。因此,第二部分发送现在有两个回调,然后第三部分会有更多回调,依此类推,呈指数增长。

Ok, so after using Debug.WriteLine, I realized that I was being an idiot.
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文