1

I am not sure what is wrong in this code.Could someone please help me why i see error while running this code?

Here is my handler code:

public class TestHandler1 : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpClient client = new HttpClient();// Creating a client and calling destination url. I dont want to call base.SendAsync(request) because I dont want to go next handler or controller. Not sure if this is the problem here.
            return await client.SendAsync(request);
        }
    }

And when I run my UT, I see error “System.InvalidOperationException: The request message was already sent. Cannot send the same request message multiple times.”

[TestMethod]
        public async Task TestHandler()
        {
            TestHandler1 testHandler = new TestHandler1();

            HttpClient httpClientForTest = new HttpClient(testHandler);
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://1.1.1.1/App/Service");
            var response = await httpClientForTest.SendAsync(httpRequestMessage);
            Assert.IsTrue(response.IsSuccessStatusCode);
        }
Shags
  • 133
  • 1
  • 1
  • 7

0 Answers0