NSwag-如何在“ ProcessResponse”方法中读取响应内容

时间:2018-11-12 13:12:51

标签: ios xamarin.forms swagger nswag

我正在通过NSwag生成RestClient。在我的应用中执行API请求时,生成的代码正确读取了响应内容:

...
ProcessResponse(client_, response_);

var status_ = ((int)response_.StatusCode).ToString();
if (status_ == "200") {
    var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

...

但是,有时我想分析响应并采取不同的行动,然后再将内容反序列化为JSON。我创建了另一个文件并实现了局部方法:

partial void ProcessResponse(System.Net.Http.HttpClient client, System.Net.Http.HttpResponseMessage response)

当我尝试读取“ ProcessResponse”方法中的响应内容时,就像在生成的类中读取响应内容一样,

await response_.Content.ReadAsStringAsync().ConfigureAwait(false)

我的应用无限期挂起。由于“ ProcessResponse”方法不是异步的,因此将调用包装到

var contentString = Task.Run(async () => await response.Content.ReadAsStringAsync()).Result;

尝试读取任务结果时,该应用程序将永远挂起。目前我完全不知所措:

  1. 为什么“ ReadAsStringAsync”在生成的代码中起作用,但在“ ProcessResponse”方法中却不起作用。
  2. 如何读取“ ProcessResponse”方法中的响应内容?

非常感谢您, 亨德里克

0 个答案:

没有答案