通过DebuggerStepThrough,DebuggerHidden不要在async-await方法中工作

通过DebuggerStepThrough,DebuggerHidden不要在async-await方法中工作

问题描述:

当您打开引发异常时中断" Studio调试器中的a>功能,它会为选定的异常类型在所有地方中断.告诉它不中断特定方法的方法是 DebuggerStepThrough 属性(或 DebuggerHidden ).

When you turn on the "Break when an exception is thrown" feature in the Visual Studio debugger it breaks everywhere for selected exception types. The way to tell it not to break in a specific method is to decorate these methods with DebuggerStepThrough attribute (or DebuggerHidden).

很明显,出于某种原因,它不适用于 async 方法.这是一个重现该问题的代码段.即使调试器 被标记为属性,它也会在 TestAsync 内中断,并且在 Test 不会中断.除外(它们之间唯一的区别是第一个是用 async 关键字标记的):

This, evidently, doesn't work for an async method for some reason. Here's a snippet that reproduces the issue. The debugger will break inside the TestAsync even though it's marked with the attributes and it will not break inside Test as excepted ( the only difference between them is the first is marked with the async keyword):

public class Attributes
{
    public async Task Run()
    {
        await TestAsync();
        await Test();
    }

    [DebuggerHidden]
    [DebuggerStepThrough]
    public async Task TestAsync()
    {
        try
        {
            throw new Exception("Async");
        }
        catch
        {
        }
        await Task.Delay(100);
    }

    [DebuggerHidden]
    [DebuggerStepThrough]
    public Task Test()
    {
        try
        {
            throw new Exception("sync");
        }
        catch
        {
        }
        return Task.Delay(100);
    }
}

那么,这种行为是故意的吗?是虫子吗?有解决方法吗?

So, is this behavior intended? Is it a bug? Is there a workaround?

属性在async/await中不能很好地发挥作用,因为异步方法在后台被重写了-并且这些属性没有遵循.有关类似情况,请参见 https://*.com/a/22412597/495262 .

Attributes don't play well with async/await since async methods get re-written under the covers--and the attributes do not follow. See https://*.com/a/22412597/495262 for a similar situation.