从 Silverlight 循环调用 WCF 服务,异步处理程序中的返回值始终相同
我的同事遇到以下问题:
Silverlight 应用程序有一个 ID 列表,并对每个 ID 进行 WCF 服务调用以查找与该 ID 匹配的对象。然后,匹配的对象通过异步“完成”处理程序返回到 Silverlight 应用程序。 Silverlight 端的调用是循环进行的,WCF 执行一些数据库访问、查找并更新对象并返回它。然后,Silverlight 完成处理程序捕获返回结果,并将对象重新填充到集合中。
问题是,由于某种原因,收到的对象列表都是同一个对象。使用断点,Silverlight 应用程序发送正确的对象,WCF 接收并返回正确的对象,但是当接收回对象时,Silverlight 应用程序不断地不断获取相同的对象(但返回正确数量的对象)。
下面的示例来说明问题(简化版本,而不是实际代码):
Private Sub sendObjects(Byval sales As List(Of Integer))
For Each saleID As Integer in sales
AddHandler hlxService.SaveBankTransactCompleted, _
AddressOf SaveBankTransactCompleted
hlxService.SaveBankTransactAsync(saleID)
End For
End Sub
Private Sub SaveBankTransactCompleted(
sender As Object,
e As SaveBankTransactCompletedEventArgs)
RemoveHandler hlxService.SaveBankTransactCompleted, _
AddressOf SaveBankTransactCompleted
saleCollection.add(e.Result)
' Check if all objects have been returned.
CheckPaymentStatus()
End Sub
最后是(简化的)WCF 服务功能:
<OperationContract()>
Public Function SaveBankTransact(
ByVal saleID as Integer) As hlxSale
Dim newSale as hlxSale
newSale = findSaleById(saleID)
' Process some data from database, put values into newSale
Return newSale
End Function
我们通过仅发送和接收整个集合来找到解决问题的方法,但仍然很好奇至于最初问题的原因是什么。
My colleague has the following problem:
The Silverlight application has a list of IDs and makes a WCF service call for each of them to find the object matching that ID. The matching object is then returned to the Silverlight app via an asynchronous "completed" handler. The calls on the Silverlight side are made in a loop, and the WCF performs some database access, finds and updates the object and returns it. The Silverlight completed handler then catches the returns, and populates the objects back into a collection.
The problem is that for some reason the list of objects received back are all the same object. Using breakpoints, the Silverlight app sends the correct objects, the WCF receives and returns the correct objects, but when receiving the objects back, the Silverlight app keeps getting the same object over and over (but the correct number of objects is returned).
Sample below to illustrate the problem (a simplified version, not the actual code):
Private Sub sendObjects(Byval sales As List(Of Integer))
For Each saleID As Integer in sales
AddHandler hlxService.SaveBankTransactCompleted, _
AddressOf SaveBankTransactCompleted
hlxService.SaveBankTransactAsync(saleID)
End For
End Sub
Private Sub SaveBankTransactCompleted(
sender As Object,
e As SaveBankTransactCompletedEventArgs)
RemoveHandler hlxService.SaveBankTransactCompleted, _
AddressOf SaveBankTransactCompleted
saleCollection.add(e.Result)
' Check if all objects have been returned.
CheckPaymentStatus()
End Sub
and lastly the (simplified) WCF service function:
<OperationContract()>
Public Function SaveBankTransact(
ByVal saleID as Integer) As hlxSale
Dim newSale as hlxSale
newSale = findSaleById(saleID)
' Process some data from database, put values into newSale
Return newSale
End Function
We found a way around the problem by just sending and receiving the collection as a whole, but would still be curious as to what was the cause of the original problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最有可能的是浏览器的缓存阻止了您的调用,甚至无法访问服务器
您需要在服务器上设置过期时间。
以下是针对 C# 的,但在 VB.NET 中应该非常相似
另一个选项是设置无缓存标头,但过期更好,因为它将有助于防止客户端滥用
Most probably it is the browser's cache is preventing from your call to even go out to the server
You need to set expiration on the server.
The following is for C# but it should be fairly similar in VB.NET
Another option is to set the no-cache header but expiration is better as it will help prevent abuse by clients