Miika L.
Miika L.

Reputation: 3353

Calling a WCF service in loop from Silverlight, return value in asynch handler is always the same

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.

Upvotes: 0

Views: 608

Answers (1)

Arnon Rotem-Gal-Oz
Arnon Rotem-Gal-Oz

Reputation: 25909

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

var nextCycle = DateTime.Now.AddSeconds(pollingInterval).ToUniversalTime();
var expires = nextCycle.ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'");
var headers = WebOperationContext.Current.OutgoingResponse.Headers;
headers.Add(HttpResponseHeader.Expires, expires);

Another option is to set the no-cache header but expiration is better as it will help prevent abuse by clients

Upvotes: 1

Related Questions