0

Whilst debugging, using Unit Tests, the function returns the expected data, however when the same function is called from JavaScript, the function is hit but then doesn't return any data.

This function that I'm calling that's in the dll is hanging, but only when it is called by a function that has been called by a JS request, why would this be?

EDIT:

As in comments, my best guess is that it is something to do with a thread being in use, but I don't know, as the function itself is working, just not when called from a C# function called by AJAX.

AJAX call :

function getOnHoldTickets() {
    $.ajax({
        type: "GET",
        url: "/cloud/getTicketCount/",
        dataType: "json",
        success: function (data) {
            onHoldHandler(data);
        },
        failure: function () {
            alert("getOnHoldTickets failled");
        }
    });
}

Controller :

// api gets hit from the JS call
[Route("cloud/getTicketCount")]
public List<UberTicket> getTicketCount()
{
    var tickets = Dashboard.getTODTickets("On Hold"); 
    return tickets;
}


[TestMethod] // calls the same method as JS
public void supportTicketTesting()
{
    var openTickets = Dashboard.getTODTickets("On Hold");
    var check = openTickets != null;
}

// method calling the dll
public static List<UberTicket> getTODTickets(string type)
{
    var tickets = UberAPI.getTODTickets(type);
    return tickets;
}

DLL Method:

// the method within the dll that's hanging when called by a function invoked by JS
public static async Task<RootObjectClass<T>> genericGet<T>(string function, string parameters)
{
    try
    {
        // create credentials to pass to httpClient
        var httpClientCredentials = new HttpClientHandler()
        {
            Credentials = new NetworkCredential(uberAPIUser, uberAPIPass)
        };

        using (var client = new HttpClient(httpClientCredentials))
        {
            // unsure if the headers are being passed in correctly - getting good response though
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
            var response = await client.GetAsync(ubersmithURL + function + parameters);
            var result = await response.Content.ReadAsStringAsync();
            // remove nulls from json
            result = Regex.Replace(result, ":null,", ":\"\",");
            var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
            var success = JsonConvert.DeserializeObject<RootObjectClass<T>>(result);
            return success;
        }
    }
    catch(Exception ex)
    {
        // log error
    }
    return new RootObjectClass<T>();
}
PurpleSmurph
  • 2,055
  • 3
  • 32
  • 52
  • 1
    is that WebAPI or MVC? – madoxdev Sep 29 '17 at 09:58
  • You should not be returning c# data types, instead return JsonResult and parse your list as Json. – Adrian Sep 29 '17 at 10:09
  • It looks like that API requires a username and password. Are you sure that the credentials are correctly configured when running the code as a website? – JLRishe Sep 29 '17 at 10:10
  • The username and password are hardcoded in the dll. It also works using the unit test so it can't be that. My best guess is something to do with a thread being in use but I have very little knowledge about this. – PurpleSmurph Sep 29 '17 at 10:29
  • It is MVC @MadOX – PurpleSmurph Sep 29 '17 at 10:29
  • @PurpleSmurph then answer below seems to be what I would suggest. Make sure you are returning JSON from the action, as JS expects JSON. – madoxdev Sep 29 '17 at 11:25

1 Answers1

1

While this might not be addressing the issue fully, you should not be returning C# Data types as JavaScript won't be able to parse them and will result in an error similar to this in the worst case

System.Collections.Generic.List`1[...]

Like I said in my comment, you should return a JsonResult from your controller to retrieve the data in JS.

[Route("cloud/getTicketCount")]
public JsonResult getTicketCount()
{
    var tickets = Dashboard.getTODTickets("On Hold"); 
    return Json(tickets ,JsonRequestBehavior.AllowGet);
}

Understanding JsonRequestBehavior.AllowGet

and your Ajax call

$.ajax({
    type: "GET",
    url: "/cloud/getTicketCount/",
    dataType: "json"
})
.done(function(data){
   console.log(data);
})
.fail(function(xhr){
    console.log(xhr.responseText); 
});

Edit:

I believe this is a deadlock issue you have. Perfect answer elaborating the issue is here.

Adrian
  • 8,271
  • 2
  • 26
  • 43
  • This doesn't address the issue at all. – JLRishe Sep 29 '17 at 10:19
  • @JLRishe I am/was pretty convinced that passing objects such as List would result in an error I have specified above. Not an actual error, but would return a string of the type returned hence the need for serialization. If you could please elaborate for my own learning purposes, thanks. – Adrian Sep 29 '17 at 10:27
  • Thank you, this may become an issue further down the road which I'll address once this is sorted.. However, JavaScript isn't throwing an error at the moment, as it isn't getting the data, `genericGet` is freezing/hanging when the function is called by a function called by JS. – PurpleSmurph Sep 29 '17 at 10:31
  • @PurpleSmurph Seeing your post again, I think it's a deadlock since data is returned from the DLL I could be wrong though. Take a look at the edit. – Adrian Sep 29 '17 at 10:48
  • That seems likely, thanks for the link, will have a look. – PurpleSmurph Sep 29 '17 at 11:10