1

I have an MVC application that calls a WebAPI async, both on POST and GET. When running both the WebAPI and MVC applications locally, the WebAPI response shows successful but the SendAsync request returns 500. Also, Fiddler doesn't show the API call. I have a suspicion that it has to do with how the async request is being handled, but I'm not sure what I'm missing.

MVC Controller call to the API:

public Model UploadFile(Model formCollection)
{
    var documentModel = formCollection.ToString();

    var client = new HttpClient();
    var uri = new Uri(BaseUri + "document/");

    var content = new StringContent(documentModel);

    var request = new HttpRequestMessage(HttpMethod.Post, uri) {Content = content};

    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    var response = client.SendAsync(request);
    response.Wait();

    try
    {
        var returned = response.Result;
        if (returned.StatusCode != HttpStatusCode.OK)
        {
            throw new Exception(returned.RequestMessage.ToString());
        }

        var model = JsonConvert.DeserializeObject<Model>    (returned.Content.ReadAsStringAsync().Result);
        model.FileContents = "";

        return model;
    }
    catch(Exception e)
    {
        var error = new Exception("Service failure - ", e);
        throw error;
    }
}

The WebAPI Post:

[HttpPost]
public async Task<HttpResponseMessage> Post([FromBody]Model model)
{
    var response = await SubmitNew(model);

    return Request.CreateResponse(response);
}

Setting the breakpoint on the return in the Post shows a valid response, but on the response.Result in the MVC Controller it shows 500. I've even tried returning an OK request no matter the response as below:

return Request.CreateResponse(HttpStatusCode.OK, result);

Any ideas as to why the client is showing 500?

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Kirby Kernen
  • 123
  • 3
  • 11
  • then wrap var response = client.SendAsync(request); with a try-catch block and see the exception – Daniel Dec 23 '16 at 15:48
  • An error 500 means that something is incorrectly configured with your WebAPI. Do you have a simpler WebAPI get method to test that helps you confirm API is Working? – Dalorzo Dec 23 '16 at 15:51
  • 1
    Use a POSTMAN/Advanced rest client to make this call and you will be able to see the error details in the response body – Shyju Dec 23 '16 at 15:51
  • use postman as mentioned above or instead of throwing exception with the RequestMessage (which does not contain the actual response), try read the response .Content.ReadAsStringAsync() and inspect that. – MichaC Dec 23 '16 at 15:58
  • I found the issue, there was a timeout error in the WebAPI's logging service that was happening after the initial POST and GET calls result.. Issue is resolved. – Kirby Kernen Dec 23 '16 at 17:56

3 Answers3

2

In your Global.asax.cs

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // Try adding the following lines:
        var configuration = GlobalConfiguration.Configuration;
        var formatters = configuration.Formatters;
        formatters.Clear();
        formatters.Add(new JsonMediaTypeFormatter());
    }

    // A good idea to have this:
    protected void Application_Error()
    {
        Exception unhandledException = Server.GetLastError();
        // Set a breakpoint here or do something to log the exception
    }

The code added to Application_Start will ensure that serialization is only to/from JSON. While it may not be what you want in your final code, it may be helpful as a temporary measure to isolate the cause of your problem.

Adding Application_Error should help to catch issues which occur within the WebAPI layer, such as when it serializes the object you've returned from your controller method.

My suspicion is that SubmitNew is returning something which cannot be serialized such as an infinitely recursive reference (example: parent/child structure with mutual references).

Zenilogix
  • 1,318
  • 1
  • 15
  • 31
1

I found the issue, there was a timeout error in the WebAPI's logging service that was happening after the initial POST and GET calls result.. Issue is resolved.

Kirby Kernen
  • 123
  • 3
  • 11
0

I had a similar issue with some code that I "improved" - I saw HttpResponseMessage implements the IDisposable interface so decided to wrap the Return Request.CreateResponse method is a using statement, leading to a 500 error.

Removing the using statement fixed the issue for me.

Liam
  • 5,033
  • 2
  • 30
  • 39