当请求的 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);
}
请问如何解决这个问题?
HttpResponseMessage
需要 some/any RequestMessage
=> _response = new HttpResponseMessage(httpStatusCode) {
RequestMessage = new(),
Content = new StringContent("Yay!")
};