首页 新闻 搜索 专区 学院

Refit NullReferenceException 问题

0
悬赏园豆:30 [待解决问题]

当请求的 web api 对应的主机名不存在时, Refit 会抛出下面的 null 引用异常

System.NullReferenceException: Object reference not set to an instance of an object.
   at Refit.RequestBuilderImplementation.<>c__DisplayClass14_0`2.<<BuildCancellableTaskFuncForMethod>b__0>d.MoveNext() in /_/Refit/RequestBuilderImplementation.cs:line 298

通过 Refit 的源代码 RequestBuilderImplementation.cs#L298 发现是 resp.RequestMessage!.Method 引起的

try
{
    return await DeserializeContentAsync<T>(resp, content, ct).ConfigureAwait(false);
}
catch (Exception ex)
{
    throw await ApiException.Create("An error occured deserializing the response.", resp.RequestMessage!, resp.RequestMessage!.Method, resp, settings, ex);
}

请问如何解决这个问题?

dudu的主页 dudu | 高人七级 | 园豆:36310
提问于:2022-09-16 14:22
< >
分享
所有回答(1)
0

HttpResponseMessage需要 some/any RequestMessage

=> _response = new HttpResponseMessage(httpStatusCode) {

  RequestMessage = new(),

  Content = new StringContent("Yay!")

};

Biuget-Golang | 园豆:311 (菜鸟二级) | 2022-09-19 09:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册