c# - 来自 DelegatingHandler 的“无法多次发送相同的请求消息”错误
问题描述
我不确定这段代码有什么问题。有人可以帮助我为什么在运行这段代码时看到错误吗?
这是我的处理程序代码:
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);
}
}
当我运行我的 UT 时,我看到错误“System.InvalidOperationException:请求消息已发送。不能多次发送相同的请求消息。”</p>
[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);
}
解决方案
推荐阅读
- bash - 在命令中的命令中带有参数的命令
- php - 如何从 php 中的逗号分隔列表生成 sendgrid json 格式
- node.js - Koa 如何帮助避免“猴子补丁”以及“Hapi”或“Express”如何不做同样的事情?
- c++ - 未初始化的局部变量错误,即使我认为我已初始化它(C++)
- javascript - 如何在简易饼图中添加延迟?
- c# - FCM 令牌检索 [Xamarin] - 由于不推荐使用的方法而更新
- java - React-native-uploader Android 错误
- ruby - 如何在 ruby 中跳转到一行
- php - 为什么我不能从服务器变量创建 PHP 标头?
- angular - 从 FileReader 传递图像以形成 Angular 6 中的输入