DebuggerStepThrough,DebuggerHidden不要在异步等待的方法工作

DebuggerStepThrough,DebuggerHidden不要在异步等待的方法工作

问题描述:

当您打开打破的时候抛出一个异常功能在Visual Studio调试它打破了处处为选定的异常类型。的方式来告诉它不要在一个特定的方法破解是装饰这些方法与 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).

本,显然的,不会因为是异步方法,出于某种原因。 这里有一个片段,重现该问题。调试器的将会的内线突破在 TestAsync 即使它标明的属性,它会的没有的内线突破测试的除外(它们之间唯一的区别是第一个标有异步关键字):

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?

属性不与异步发挥好/等待,因为异步方法得到重新写在幕后 - 和属性不遵循。请参见 http://*.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 http://*.com/a/22412597/495262 for a similar situation.