我试图抑制跨异步线程的执行上下文的流动。我写了下面的代码,但它抛出了一个错误 -
InvalidOperationException: AsyncFlowControl object must be used on the thread where it was created.
System.Threading.AsyncFlowControl.Undo()
Web1.Controllers.ValuesController+<Get>d__0.MoveNext() in ValuesController.cs
+
throw;
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
当我通过代码进行调试时,不会引发错误。它仅在我不调试时发生。
示例代码:
[HttpGet]
public async Task<IActionResult> Get()
{
try
{
using (ExecutionContext.SuppressFlow())
{
await Switch.Instance.SwitchOn();
}
}
catch (Exception ex)
{
var x = ex.Message;
throw;
}
return Ok("Done!");
}
我是不是走错了路?