0

I have 2 separate applications, an MVC5 UI and a Asp.net WebApi2 (not asp.net core). I can read data from the API just fine. Now I want to send data from a form in the UI to the API, so it can write it to it's database.

For internal reasons, the MVC controller receives a POST request from a form and then is supposed to update the data in the database by sending it to the API and display some form of result (error or confirmation). The API receives the data, does it's work and responds with a 200 and the desired answer ("updated").

Sadly, the UI does somehow not realize this answer and keeps on waiting forever... I have read there would be a timeout at 90 seconds, but that doesn't seem to be the case. I have to emphasize that I am a newbie, so I guess I made some weird mistake :D

The code I used worked fine from my console application, but either I messed it up or it does not work at all from WebApi? I tried setting the timeout, but it doesn't change anything :/

The last log entry is: sending update request to api...

Controller Code:

[HttpPost]
    public ActionResult EditTicket(ServiceManagementTicket postedTicket)
    {

        if (!ModelState.IsValid)
        {
            // some error handling code
        }


        string TicketUpdateResult = updateTicket(postedTicket).Result;

        if (TicketUpdateResult == "updated")
        {

            return View("SingleTicketView", postedTicket);
        }
        else
        {

            Log.Verbose("ticket could not be updated. The error should have been logged");
            return View("Views/Pages/InternalServerError");

        }
    }

Task Code:

    public static async Task<string> updateTicket(ServiceManagementTicket postedTicket)
    {


        string FinalEndpoint = "api url here"


        String jsonToExport = JsonConvert.SerializeObject(postedTicket);
        StringContent jsonToExportConverted =
            new StringContent(jsonToExport, Encoding.UTF8, "application/json");

        Log.Verbose("Trying to update ticket at: " + FinalEndpoint);


        try
        {

            using (var client = new HttpClient())
            {

                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                client.Timeout = TimeSpan.FromSeconds(5);

                Log.Verbose("sending update request to api...");

                HttpResponseMessage response = await client.PutAsync(
                    FinalEndpoint,
                    jsonToExportConverted);


                Log.Verbose("Updating the Ticket at the webapi was: " + response.ReasonPhrase);

                if (response.ReasonPhrase == "updated")
                {

                    Log.Verbose("api confirmed the update");
                    return response.Content.ReadAsStringAsync().Result;

                }

                Log.Verbose("something ent wrong :/");
                return "error";

            }
        }
        catch (Exception e)
        {
            // error handling
        }

    }

Sending the data to the api in a synchronous call would be fine as well, I just couldn't find an example that worked for me.

halfer
  • 19,824
  • 17
  • 99
  • 186
soomon
  • 396
  • 2
  • 18

1 Answers1

1

The lines of code below will deadlock in ASP.NET applications, never use Task.Result in an ASP.NET application.

string TicketUpdateResult = updateTicket(postedTicket).Result;

return response.Content.ReadAsStringAsync().Result;

Those lines of code should be:

string TicketUpdateResult = await updateTicket(postedTicket);

return await response.Content.ReadAsStringAsync();
Nathan Werry
  • 876
  • 7
  • 18