1

Im having trouble with this, I dont why, it must be something im doing wrong. I've had to rewrite to get it working but it smells all wrong, however it works.

So this is what I tried first and it does not work, as the statuscode of 500 was returned, but this is because it was not waiting for a response, I need it to wait

[HttpPost]
public async Task<JsonResult> Booking(string model)
{
    //do some bits.
    var a = new JavaScriptSerializer().Serialize(e);
    var booking = new HttpClient();
    HttpContent content = new StringContent(a,Encoding.UTF8);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    HttpResponseMessage response = await booking.PostAsync("https://webapi.domain.com/Booking/Post", content);
    var aa = response.StatusCode //500 Internal Error
}

So I rewrote

[HttpPost]
public async Task<JsonResult> Booking(string model)
{
    //do some bits.
    var a = new JavaScriptSerializer().Serialize(e);
    var booking = new HttpClient();
    HttpContent content = new StringContent(a,Encoding.UTF8);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    HttpResponseMessage response = await booking.PostAsync("https://webapi.domain.com/Booking/Post", content);
    var t = new Stopwatch();

    while (response.StatusCode ==HttpStatusCode.InternalServerError)
    {
        t.Start();
        var zzzz = response.ReasonPhrase;
        if (t.ElapsedMilliseconds >10000)
        {
            response.StatusCode = HttpStatusCode.RequestTimeout;
            t.Stop();
        }
    }
    var aa = response.StatusCode //201 Created
}

And this works and returns me my 201, ugly, but can anyone tell me and show me what i'm doing wrong?

12
  • 2
    Please format the code! Commented Sep 21, 2016 at 11:06
  • what is booking.PostAsync ? Why don't you directly go there ? Commented Sep 21, 2016 at 11:06
  • 1
    I find it really unlikely that the second piece of code works when the first one does not. Are you sure? The response status code does not change after some time. The HTTP protocol has no way to "update" a response that was sent. All this timing stuff does nothing. Commented Sep 21, 2016 at 11:22
  • A status code of 500 generally means something went wrong on the server, in .net (example: web service or web api) this usually indicates an un-handled Exception occurred. Your first example (provided code) looks technically fine and should work. You should debug the end point webapi.domain.com/Booking/Post to figure out what is going wrong (unexpected passed in data, data not in correct format, data missing, network issue, who knows). Your "work around" (code part 2) does not fix the problem. Commented Sep 21, 2016 at 11:31
  • 1
    This is highly unlikely. Start Fiddler and set it as a proxy in web.config (this is easy). Make sure that Fiddler agrees with your assessment of the response codes. Then, find out the difference in the requests you are sending. I'd bet money there is a difference, or your interpretation of the responses is wrong. Commented Sep 21, 2016 at 11:57

1 Answer 1

1

The server has a timing dependent bug. It goes away when you use the debugger to give it enough time to avoid the crash.

An asynchronous module or handler completed while an asynchronous operation was still pending

Looks like something with async.

The client is not at fault and the while (response.StatusCode ==HttpStatusCode.InternalServerError) loop does nothing. It does not even change the timing at the server. Your usage of the debugger probably did that and the two effect were confounded in your interpretation.

Fix the server, now that you know where to look.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Ive narrowed it down to the sending of an email (the email actually sends, no error is thrown in debug) public bool SendMail(string from, string to, string subject, string body, bool html) {...} any ideas?
Well, strictly speaking the question is solved because the client side problem is solved (it does not exist). The best course of action would be to ask a question with details about the server crash. But it looks like you are starting some async action and not waiting for completion. That's what the message says. So audit the code for such a thing. Anything with "async" in its name is interesting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.