![](/img/trans.png)
[英]Async and await: Why doesn't an async method whose signature defines a return type of Task explicitly return a Task?
[英]Async Method Doesn't Return
我有以下Action方法,它使用Scanner
类,它使用一些webservice来获取一些数据。 当我在GetSuggestions
方法中使用断点时,可以看到结果。 但是,此数据永远不会返回到我的Action方法。 相反,当我检查Index()
的model
的值时,它是,
Id = 1, Status = System.Threading.Tasks.TaskStatus.WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
我检查了这个问题,但它没有帮助我。
控制器动作方法:
[HttpGet]
public ActionResult Index()
{
var plane = new Scanner();
var model = plane.GetSuggestions("ISTANBUL");
return View(model);
}
GetSuggestions方法:
public async Task<List<PlaceDto>> GetSuggestions(string key)
{
string url = String.Format("URL GOES HERE", key, API_KEY);
string data = await RequestProvider.Get(url);
return JObject.Parse(data).SelectToken("Places").ToObject<List<PlaceDto>>();
}
RequestProvider方法:
public static async Task<string> Get(string url)
{
using (var client = new HttpClient())
{
return await client.GetStringAsync(url);
}
}
编辑1
我也尝试用Action包装Action方法并等待GetSuggestions
方法,但我在client.GetStringAsync(url)
上收到异常
当我在“操作方法”上使用“任务”时发生异常
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=System.Web
StackTrace:
at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
at System.Web.LegacyAspNetSynchronizationContext.Post(SendOrPostCallback callback, Object state)
at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.PostAction(Object state)
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.AwaitTaskContinuation.<ThrowAsyncIfNecessary>b__1(Object s)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
InnerException:
编辑2
我从await client.GetStringAsync(url);
删除了await关键字await client.GetStringAsync(url);
和代码工作。 但是,我认为这将同步运行而不是异步运行。 以下是更新的Get方法,
public static async Task<string> Get(string url)
{
using (var client = new HttpClient())
{
return client.GetStringAsync(url).Result;
}
}
你的问题分为两部分。
首先是僵局。 您需要使用await
替换任何Task.Wait
或Task<T>.Result
调用。 我在博客和您链接的答案中更充分地解释了这种死锁情况。
第二个例外。 您正在看到此异常,因为您在ASP.NET 4.0上使用async
/ await
,这是不受支持的。 您需要确保以.NET 4.5为目标,并在web.config中设置<httpRuntime targetFramework="4.5" />
。
我想你需要等待plane.GetSuggestions("ISTANBUL");
的召唤plane.GetSuggestions("ISTANBUL");
[HttpGet]
public async Task<ActionResult> Index()
{
var plane = new Scanner();
var model = await plane.GetSuggestions("ISTANBUL");
return View(model);
}
当您调试代码时,由于逐步执行代码,它可能会按预期运行。 一旦你让代码自然运行,自从调用plane.GetSuggestions("ISTANBUL");
是等待,因此继续调用return View(model);
在plane.GetSuggestions("ISTANBUL");
有机会完成。
我认为这就是为什么你没有得到预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.