gpt4 book ai didi

asp.net - 异步代码,无需等待完成

转载 作者:行者123 更新时间:2023-12-04 12:32:41 24 4
gpt4 key购买 nike

我目前遇到IIS崩溃的问题,并在事件日志中留下了以下消息。他们并不能帮助我找到错误的实际来源,但是一些研究表明,这只是生成任务的一种情况,而不是等待结果,当它们在父流程完成后最终完成时就无法完成与导致空引用异常的父线程相关联。那是对的吗?

在大多数情况下,我已删除或添加了等待操作的地方,但是在某些地方它很方便。在 session 开始时,一个这样的例子就是内存缓存 session 特有的一些细节,但是它们并不是立即重要的,因此我启动了一项任务来完成这项工作,但不要等待它。

I have added ConfigureAwait(false),这足以防止将来发生错误吗?听起来这将删除线程切换,我认为这将防止该错误。

Task.Run(() =>
{
// Do caching here

}).ConfigureAwait(false);

ASP.NET 4.0.30319.0
Exception: System.NullReferenceException

Message: Object reference not set to an instance of an object.

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.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.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()

.NET运行时
Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
at System.Threading.Tasks.AwaitTaskContinuation.<ThrowAsyncIfNecessary>b__1(System.Object)
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()

应用程序错误
Faulting application name: w3wp.exe, version: 7.5.7601.17514, time stamp: 0x4ce7afa2
Faulting module name: KERNELBASE.dll, version: 6.1.7601.18015, time stamp: 0x50b8479b
Exception code: 0xe0434352
Fault offset: 0x0000000000009e5d
Faulting process id: 0x1d98
Faulting application start time: 0x01ceaf8ea10ece66
Faulting application path: c:\windows\system32\inetsrv\w3wp.exe
Faulting module path: C:\Windows\system32\KERNELBASE.dll
Report Id: 341b9a76-1b82-11e3-8e17-005056be0005

最佳答案

经过一整天的追踪,我终于发现了一条可以让 parent 绕开等待的途径,以解决这个问题。快速重构以防止这种情况解决了该问题。

导致问题的代码以最简单的形式遵循以下原则:

var getDataTask = getData();

if(some_condition == true) {
return some_object;
}

getDataTask.Wait();

return getDataTask.result;

如果是 some_condition == true,则该方法将返回而无需等待 getDataTask完成。重构以阻止该问题的修复。

关于asp.net - 异步代码,无需等待完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18759987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com