【发布时间】:2018-05-15 07:47:15
【问题描述】:
我对@987654321@ 和Task<HttpResponseMessage> 有点困惑。
如果我使用HttpClient 方法PostAsync() 发布数据,我需要将Task<HttpResponseMessage> 而不是HttpResponseMessage 作为返回值,据我所知。
如果我使用Request.CreateResponse(HttpStatusCode.Forbidden, myError.ToString());
那么我只得到响应消息对象而不是Task 对象。
所以我的问题是如何为对 web api 方法的异步调用创建拟合返回?
(因此我的理解是正确的,如果是这样,如何最好地将消息对象转换为 Task<HttpResponseMessage> 对象)
原代码:
public HttpResponseMessage DeviceLogin(MyDevice device)
{
EnummyError myError = EnummyError.None;
// Authenticate Device.
myError = this.Authenticate(device);
if (myError != EnummyError.None)
{
return Request.CreateResponse(HttpStatusCode.Forbidden, myError.ToString());
}
}
更新的方法头:
public Task<HttpResponseMessage> DeviceLogin(MyDevice device)
【问题讨论】:
-
我没有关注你的问题。你有一些代码可以解释这个问题吗?你自己并没有真正对任务做任何事情,框架会处理它,你只是返回你无论如何都会返回的类型。
-
您使用的是 Web Api 版本 1 还是 2?
-
@Baksteen Visual Studio 2017 所以 v2 会在一秒钟内更新示例代码
标签: c# asynchronous asp.net-web-api async-await