Ich habe eine Webseite (aspx), die eine asynchrone Methode in einer Klasse aufruft. Die Asynchron-Methode sieht wie folgt aus:Ist es in Ordnung, vor dem endgültigen Warten von einer asynchronen Methode zurückzukehren?
public async Task<MyThing> GetMyThing()
{
MyThing thing = new MyThing();
var stuff = await Repository.GetStuff();
// apply stuff to thing.
var morestuff = await Repository.GetMoreStuff();
// apply more stuff to thing.
if (someCondition)
{
return thing;
}
var yetMoreStuff = await Repository.GetYetMoreStuff();
// apply yet more stuff
return thing;
}
Wenn die Bedingung erfüllt ist, ich weiß nicht, die Daten aus dem letzten Repository Anruf benötigen. Wird mir die Rückkehr vor dem letzten Warten Probleme bereiten?
Der Grund, warum ich frage bin ich diesen Fehler in dem Server-Anwendungsprotokoll bin immer, und ich bin die Jagd nach Ursachen ...
Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean)
at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(System.Threading.SendOrPostCallback, System.Object)
at System.Web.LegacyAspNetSynchronizationContext.CallCallback(System.Threading.SendOrPostCallback, System.Object)
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback, System.Object, System.Threading.Tasks.Task ByRef)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
Wenn Sie 'zurückkommen', wird' 'erwarten' 'überhaupt nicht passieren. – SLaks
müssen Sie diesen Code durchlaufen. Es ist ein NRE-Problem. – Nkosi
Der Fehler ist nicht wegen des Threads, aber eine Ausnahme in Ihrem Repository sprudelt. – Chad