emr*_*azi 2 .net c# asp.net-mvc async-await
我有以下Action方法,它使用Scanner类使用一些webservice来获取一些数据.当我在GetSuggestions方法中使用断点时,我可以看到结果.但是,此数据永远不会返回到我的Action方法.相反,当我检查model内部的值时Index(),它是,
Id = 1, Status = System.Threading.Tasks.TaskStatus.WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
Run Code Online (Sandbox Code Playgroud)
我检查了这个问题,但它没有帮助我.
控制器动作方法:
[HttpGet]
public ActionResult Index()
{
var plane = new Scanner();
var model = plane.GetSuggestions("ISTANBUL");
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
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>>();
}
Run Code Online (Sandbox Code Playgroud)
RequestProvider方法:
public static async Task<string> Get(string url)
{
using (var client = new HttpClient())
{
return await client.GetStringAsync(url);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑1
我还尝试使用task包装Action方法并等待GetSuggestions方法,但是我收到了异常client.GetStringAsync(url)
当我使用Task on Action Method时出现异常
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:
Run Code Online (Sandbox Code Playgroud)
编辑2
我删除了await关键字await client.GetStringAsync(url);和代码工作.但是,我认为这将同步运行而不是异步.以下是更新的Get方法,
public static async Task<string> Get(string url)
{
using (var client = new HttpClient())
{
return client.GetStringAsync(url).Result;
}
}
Run Code Online (Sandbox Code Playgroud)
你的问题分为两部分.
首先是僵局.您需要替换任何Task.Wait或Task<T>.Result呼叫await.我在博客和您链接的答案中更充分地解释了这种死锁情况.
第二个是例外.您看到此异常是因为您在ASP.NET 4.0上使用async/ await,这是不受支持的.您需要确保定位.NET 4.5并且已<httpRuntime targetFramework="4.5" />在web.config中设置.
| 归档时间: |
|
| 查看次数: |
5372 次 |
| 最近记录: |