我在退回"立即"时遇到了问题。来自通常异步ApiController的响应。代码如下,寻找感叹号。用例是内容类型检查失败,我想发回错误响应消息。第一个版本挂起了Visual Studion 2010(和Fiddler)。第二部作品。
我的问题是,为什么我不能使用我的初始方法返回仅仅传回响应对象的虚拟任务?
public class MyController : ApiController
{
public Task<HttpResponseMessage> Post([FromUri]string arg)
{
HttpResponseMessage response = null;
// synchronous validation
if (Request.Content.Headers.ContentType.MediaType != @"image/jpeg")
{
response = Request.CreateErrorResponse(
HttpStatusCode.UnsupportedMediaType,
"Invalid Content-Type.");
}
if (response == null) // no immediate response, switch to async
{
// work done here
}
else // immediate response, but we need to wrap in a task for caller to fetch
{
// !!!! this one doesn't work !!!
return new Task<HttpResponseMessage>( () => response);
// !!! this one does !!!
TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>();
tcs.SetResult(response);
return tcs.Task;
}
}
}
答案 0 :(得分:8)
Task<T>
的构造函数返回尚未启动的任务,为了使其工作,您必须执行以下操作:
var task = new Task<HttpResponseMessage>(() => response);
task.Start();
return task;
但这样做效率很低,因为不必要地在线程池上执行lambda。你的第二个版本更好,在.Net 4.5中,更好的版本是使用Task.FromResult()
:
return Task.FromResult(response);