HttpReponseMessage 的 StatusCode 为 200,但 'Result' 为空
HttpReponseMessage has StatusCode 200 but 'Result' is empty
我目前正在开发一个与 Web API 集成的应用程序。我有一个异步函数,它向 Web API 发送 PUT
请求以更新产品。
public async Task<ResponseStatus> updateProduct(Item product)
{
string URL = client.BaseAddress + "/catalog/products/" + product.id;
HttpResponseMessage response = await client.PutAsJsonAsync(URL, product).ConfigureAwait(false);
var payload = response.Content.ReadAsStringAsync();
ResponseStatus res = JsonConvert.DeserializeObject<ResponseStatus>(await payload.ConfigureAwait(false));
return res;
}
在我用来测试我的应用程序的控制台应用程序中,此功能完全按预期工作,我在响应的 Result
中收到更新后的产品作为 JSON 字符串。
然而,当我尝试从 ASP.Net 应用程序调用此函数时,我在 Result
中收到一个空字符串,即使它的状态代码为 200。我还可以看到我的更新没有对产品生效,即使它在控制台应用程序中调用时肯定会更新。
我有一些 Web API 调用,例如这个 GET
请求,它们的工作方式几乎完全相同,除了它在 ASP.Net 应用程序中工作并且测试控制台应用程序。
public async Task<Product> getProductByID(int id)
{
Product product = null;
string URL = client.BaseAddress + "/catalog/products/" + id;
string additionalQuery = "include=images,variants";
HttpResponseMessage response = await client.GetAsync(URL + "?" + additionalQuery).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
var payload = response.Content.ReadAsStringAsync();
product = JsonConvert.DeserializeObject<Product>(await payload.ConfigureAwait(false));
}
return product;
}
为什么在 ASP.Net 应用程序中我会收到成功状态代码,即使 Result
为空并且实际上并未对产品进行更新?
您需要 await
payload Task
而没有 ConfigureAwait(false)
。
通过使用 ConfigureAwait(false)
,您无法保证在 Task
returns Result
时保持 ASP.NET 响应上下文。这是 ASP.NET 运行时的一个特点。
它被称为 Best Practice tip for async/await by Microsoft:
You should not use ConfigureAwait when you have code after the await in the method that needs the context. For GUI apps, this includes any code that manipulates GUI elements, writes data-bound properties or depends on a GUI-specific type such as Dispatcher/CoreDispatcher. For ASP.NET apps, this includes any code that uses HttpContext.Current or builds an ASP.NET response, including return statements in controller actions.
对于遇到此问题的任何人。我试图通过 BigCommerce API 更新产品,但我的代码出现错误,其中产品的 custom_url
对象将其 URL 设置为 null
。
这显然在 BigCommerce API 服务器上产生了一个错误,但由于返回了成功代码 200,因此无法正确处理,即使产品没有得到更新。这产生了我得到的空响应。
我目前正在开发一个与 Web API 集成的应用程序。我有一个异步函数,它向 Web API 发送 PUT
请求以更新产品。
public async Task<ResponseStatus> updateProduct(Item product)
{
string URL = client.BaseAddress + "/catalog/products/" + product.id;
HttpResponseMessage response = await client.PutAsJsonAsync(URL, product).ConfigureAwait(false);
var payload = response.Content.ReadAsStringAsync();
ResponseStatus res = JsonConvert.DeserializeObject<ResponseStatus>(await payload.ConfigureAwait(false));
return res;
}
在我用来测试我的应用程序的控制台应用程序中,此功能完全按预期工作,我在响应的 Result
中收到更新后的产品作为 JSON 字符串。
然而,当我尝试从 ASP.Net 应用程序调用此函数时,我在 Result
中收到一个空字符串,即使它的状态代码为 200。我还可以看到我的更新没有对产品生效,即使它在控制台应用程序中调用时肯定会更新。
我有一些 Web API 调用,例如这个 GET
请求,它们的工作方式几乎完全相同,除了它在 ASP.Net 应用程序中工作并且测试控制台应用程序。
public async Task<Product> getProductByID(int id)
{
Product product = null;
string URL = client.BaseAddress + "/catalog/products/" + id;
string additionalQuery = "include=images,variants";
HttpResponseMessage response = await client.GetAsync(URL + "?" + additionalQuery).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
var payload = response.Content.ReadAsStringAsync();
product = JsonConvert.DeserializeObject<Product>(await payload.ConfigureAwait(false));
}
return product;
}
为什么在 ASP.Net 应用程序中我会收到成功状态代码,即使 Result
为空并且实际上并未对产品进行更新?
您需要 await
payload Task
而没有 ConfigureAwait(false)
。
通过使用 ConfigureAwait(false)
,您无法保证在 Task
returns Result
时保持 ASP.NET 响应上下文。这是 ASP.NET 运行时的一个特点。
它被称为 Best Practice tip for async/await by Microsoft:
You should not use ConfigureAwait when you have code after the await in the method that needs the context. For GUI apps, this includes any code that manipulates GUI elements, writes data-bound properties or depends on a GUI-specific type such as Dispatcher/CoreDispatcher. For ASP.NET apps, this includes any code that uses HttpContext.Current or builds an ASP.NET response, including return statements in controller actions.
对于遇到此问题的任何人。我试图通过 BigCommerce API 更新产品,但我的代码出现错误,其中产品的 custom_url
对象将其 URL 设置为 null
。
这显然在 BigCommerce API 服务器上产生了一个错误,但由于返回了成功代码 200,因此无法正确处理,即使产品没有得到更新。这产生了我得到的空响应。