tags:

views:

139

answers:

2

I have an ajax-enabled WCF service that returns a set of JSON objects back to the browser. The service has a simple function that calls a business layer dll. This then returns the objects to the calling method.

Below is the service implementation (minus the Imports statements):

<ServiceContract(Namespace:="")> _
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
<ServiceBehavior(IncludeExceptionDetailInFaults:=True, MaxItemsInObjectGraph:=5000)> _
Public Class NoteService

<OperationContract()> _
Public Function GetAllInsuredNotes(ByVal insuredID As Integer) As List(Of NoteExport)
    Dim allNotes As New List(Of NoteExport)

    Using nr As New NoteRepository()
        allNotes = nr.GetInsuredNotesForExport(insuredID)
        If allNotes Is Nothing Then
            Throw New InvalidOperationException("The operation to retrieve notes caused an error.")
        End If
    End Using

    Return allNotes.ToList()
End Function

The Javascript to call my service is as follows:

    function exportToExcel(sender, eventArgs) {
        var insuredID = $('input[id*=hdnInsuredID]').val();
        NoteService.GetAllInsuredNotes(insuredID, OnNoteGetSuccess, OnNoteGetFailure, null);
    }

    function OnNoteGetSuccess(result) {
        var insuredID = $('input[id*=hdnInsuredID]').val();
        OutputExcel(insuredID, result);
        return true;
    }

    function OnNoteGetFailure(result) {
        alert('There was an error retrieving notes for export.  Please contact the help desk for assistance.');
        return false;
    }

Basically my problem is this. Everything seems to work fine from the server side function standpoint. Every time I call the function client side, the server side code executes and the result is generated. However, the success callback function only gets called periodically. I can invoke the function several times and only have the callback executed once. The problem seems to grow worse the larger the result set returned.

I could understand if it was related to the MaxObjectsInGraph setting, but the problem isn't that the result never comes back if I have a large amount of data. It will come back sometimes every forth or fifth try, sometimes 2 tries in a row, sometimes 1 in ten tries. It seems very random.

I have spent at least 2 days racking my brain on this one and can't seem to discover the solution. Does anyone have any insight on this?

A: 

I don't think we can help you figure it out either unless we have the JavaScript definition for

NoteService.GetAllInsuredNotes(insuredId, CallBack1, CallBack2, WhatIsThisParam)
Mike Atlas
Well, hopefully this will clarify. I'm using the Microsoft AJAX ScriptManager and registering the web service in the script manager. The script manager sets the path as "~/WebServices/NoteService.svc which contains the first code sample above. The last parameter is the UserContext and most examples I've seen set this to null which I have done as well. Then I think the Microsoft ScriptManager creates a proxy to the web service. Does that help?
wkparry
By the way, I also changed things around a bit to use JQuery's $.getJSON function to call the web service, added "WebGet" to the service so I could use get to call it, and the same thing happens. I getter better performance and more consistent results, but as the data set returned grows, the callback is only called intermittently. If the data set is large enough then it never calls the callback function.
wkparry
+1  A: 

Ok, I figured out what was happening and thought I'd post it here in case anyone else experiences this kind of issue. Using fiddler was the tool that put me on the right track.

Basically, the link button I was using to make the call to the javascript function was calling a full page postback, not just calling the javascript function. So if the request was small enough, the response to the web service call came quickly and the web page ran the callback function with the new JSON data it received. As the data sets got larger, sometimes the response would come back in time for the page to process the results. However, sometimes the response wouldn't come back until the full page postback was complete and the reference to the callback function was lost. So it would get back the JSON data but wouldn't know what to do with it.

So I had the javascript function called by the link button always return false to cancel the post back and the problem was solved.

I only had one other issue to deal with and that was setting the MaxObjectsInGraph setting for the service to a high enough value to account for the JSON size coming back. The only thing I still find odd is that if this setting was not high enough, I would get a challenge response box asking for a login name for the first couple of attempts, then the service would just come back with an unknown status code.

In any case, I hope this post proves helpful to someone else.

wkparry