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

2

42

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;
}
Tubbs answered 30/7, 2014 at 21:28 Comment(3)
What do you pass as a parameter in func?Hatter
@I3arnon I've updated my question with the caller.Tubbs
then my guess was accurate right down to the nameHatter
H
40

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());
}
Hatter answered 30/7, 2014 at 21:38 Comment(7)
Thanks @i3arnon. I'll give that a shot and report back.Tubbs
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?Tubbs
@Tubbs 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.Hatter
That's what I was trying to do initially, but when I have content, I ran into this problem: #25044666Tubbs
@Tubbs I see. Then yes, the second call to func must create a new request instance and just set your headers before sending again.Hatter
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.Exaggeration
I have tried same approach, but still facing the issue. Can someone help me what needs to be done.Henshaw
M
7

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.

Moisture answered 13/7, 2021 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.