【发布时间】:2020-11-13 02:57:56
【问题描述】:
我在集成测试中使用 Flurl 并尝试配置客户端以记录响应(使用 Flurl.Http 3.0.0)。
我正在使用event handlers 将响应读取为字符串,然后将其记录下来。
但是,如果在启用日志记录时调用代码使用IFlurlResponse.GetJsonAsync<>,则反序列化对象为空(我想是因为流已经被读取)。
我认为这是可能的,因为我可以看到 Flurl 在内部跟踪是否已读取响应流(使用 _streamRead 和 _capturedBody 成员)。
这是一个重现,使用Flurl.Http 3.0.0:
class Program
{
static async Task Main(string[] args)
{
using (var client = new FlurlClient("https://jsonplaceholder.typicode.com/"))
{
var post = await client
.Request("posts/1")
.ConfigureRequest(settings => settings.AfterCallAsync = LogResponse)
.GetJsonAsync<Post>();
Console.WriteLine($"Is null with logging enabled: {post is null}"); // prints True
post = await client.Request("posts/1").GetJsonAsync<Post>();
Console.WriteLine($"Is null with logging disabled: {post is null}"); // prints False
}
}
private static async Task LogResponse(FlurlCall call)
{
var responseString = await call.Response.GetStringAsync();
Console.WriteLine(responseString);
}
}
public class Post
{
public int Id { get; set; }
public int UserId { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
输出:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
Is null with logging enabled: True
Is null with logging disabled: False
如果我将调用代码从使用 GetJsonAsync<> 更改为 GetStringAsync 并自己处理 json 反序列化,则响应可以“读取”两次,但更冗长。
【问题讨论】:
-
你能(至少)显示事件处理程序中发生了什么吗?
-
我不确定我是否理解这个问题,我以为我在
LogResponse中展示了实现?还是您要求别的? -
ga,对不起,当我问这个问题时一定很累。不知道我怎么错过了。
标签: flurl