0

I have an ASP.NET WebAPI Controller which creates a document in Azure DocumentDB from the data passed on to its POST method from a Console app. The return type of the POST method is HttpResponseMessage which returns a status code for OK (i.e. 200) when the document has been successfully created.

The document gets created successfully from the WebAPI and the status code 200 is returned too. But somewhere then it goes wrong (which I can't figure out where and why) and the status code that my Console app receives after the successful POST is 500 - an Internal Server Error occurred.

public HttpResponseMessage Post([FromBody]GPSDataVM data) 
{ 
    if (KeyRepository.IsValid(data.Key)) 
    { 
        // Creates a document in DocumentDB by calling an async method CreateLiveDataDocument(data); 
        return new HttpResponseMessage(HttpStatusCode.OK); 
    } 

    return new HttpResponseMessage(HttpStatusCode.Unauthorized); 
}

Can anyone help me out with the situation? Thanks in advance ..

Arnab Chakraborty
  • 7,442
  • 9
  • 46
  • 69
Abhishek Das
  • 82
  • 4
  • 11
  • If the console receives 500, how do you know the server returned 200? – Chet Oct 12 '15 at 11:57
  • Please post the code in your controller and the error response you are getting. – Arnab Chakraborty Oct 12 '15 at 16:50
  • I had put a break-point in my WebAPI Controller to check the status code which is getting returned to the Console app. There I found it to be 200. – Abhishek Das Oct 13 '15 at 06:41
  • @ArnabChakraborty - public HttpResponseMessage Post([FromBody]GPSDataVM data) { if (KeyRepository.IsValid(data.Key)) { // Creates a document in DocumentDB by calling an async method CreateLiveDataDocument(data); return new HttpResponseMessage(HttpStatusCode.OK); } return new HttpResponseMessage(HttpStatusCode.Unauthorized); } – Abhishek Das Oct 13 '15 at 06:45

1 Answers1

0

I found an answer to my problem from the following address: Web Api + HttpClient: An asynchronous module or handler completed while an asynchronous operation was still pending

I changed the definition to my WebAPI Controller to:

public **async Task<HttpResponseMessage>** Post([FromBody]GPSDataVM data) {
        if (KeyRepository.IsValid(data.Key))
        {
            // Creates a document in DocumentDB by calling an async method
            **// Code for CreateLiveDataDocument(data)**
            return new HttpResponseMessage(HttpStatusCode.OK);
        }
        return new HttpResponseMessage(HttpStatusCode.Unauthorized); }
Community
  • 1
  • 1
Abhishek Das
  • 82
  • 4
  • 11