The request message was already sent. Cannot send the same request message multiple times

27,318

Solution 1

You are calling the same func parameter twice:

var response = await ProcessRequestAsync(func);
//...
response = await ProcessRequestAsync(func);

In this case func returns the same request every single time. It doesn't generate a new one every time you call it. If you truly need a different request each time then func needs to return a new message each call:

var response = await GetAsync(() => new HttpRequestMessage()); // Create a real request.

public async Task<HttpResponseMessage> GetAsync(Func<HttpRequestMessage> requestGenerator)
{
     return await RequestAsync(() => requestGenerator());
}

Solution 2

I had the same issue, but no repetition in my code. Turned out I had added a watch on an asynchronous process. That watch called the process while I stepped through the code, so that when I got to the line I was trying to debug it crashed with this error message. Removing all watches solved the problem. Leaving this here for other people who might have the same problem.

Share:
27,318

Related videos on Youtube

Prabhu
Author by

Prabhu

Updated on July 14, 2021

Comments

  • Prabhu
    Prabhu almost 3 years

    Is there anything wrong with my code here? I keep getting this error:

    System.InvalidOperationException: The request message was already sent. Cannot send the same request message multiple times.

    My HttpRequestMessage is inside a Func so I figured I get a brand new request each time I pass in func().

    public async Task<HttpResponseMessage> GetAsync(HttpRequestMessage request)
    {
         return await RequestAsync(() => request);
    }
    
    public async Task<HttpResponseMessage> RequestAsync(Func<HttpRequestMessage> func)
    {
       var response = await ProcessRequestAsync(func);
    
        if (response.StatusCode == HttpStatusCode.Unauthorized)   
        {
            WaitForSomeTime();
            response = await ProcessRequestAsync(func);        
        }
    
        return response;
    }
    
    private async Task<HttpResponseMessage> ProcessRequestAsync(Func<HttpRequestMessage> func)
    {
        var client = new HttpClient();
        var response = await client.SendAsync(func()).ConfigureAwait(false);
        return response;
    }
    
    • i3arnon
      i3arnon almost 10 years
      What do you pass as a parameter in func?
    • Prabhu
      Prabhu almost 10 years
      @I3arnon I've updated my question with the caller.
    • i3arnon
      i3arnon almost 10 years
      then my guess was accurate right down to the name
  • Prabhu
    Prabhu almost 10 years
    Thanks @i3arnon. I'll give that a shot and report back.
  • Prabhu
    Prabhu almost 10 years
    Looks like that was it. Question--if I just want to just change the header attribute of the HttpRequestMessage after WaitForSometime(), should recreate the object again, with the new header and assign it back to the same func?
  • i3arnon
    i3arnon almost 10 years
    @Prabhu I actually see no point of using a func instead of just passing a simple parameter. Then you can try and just change the header and send again. If that fails you can create a new one with the headers and copy the relevant properties off the old one.
  • Prabhu
    Prabhu almost 10 years
    That's what I was trying to do initially, but when I have content, I ran into this problem: stackoverflow.com/questions/25044166/…
  • i3arnon
    i3arnon almost 10 years
    @Prabhu I see. Then yes, the second call to func must create a new request instance and just set your headers before sending again.
  • Larry Smith
    Larry Smith almost 5 years
    This is especially useful advice if for instance you use Polly to retry your request. If all you do is re-send the same request (the reason why I'm here!) then you get that same exception that the OP mentioned.
  • sar
    sar almost 2 years
    I have tried same approach, but still facing the issue. Can someone help me what needs to be done.