Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DebuggerNonUserCode does not work with async/await

So, I have the following code:

static void Main(string[] args)
{
    var test1 = GeneratedHelperSync();
    var test2 = GeneratedHelperAsync().Result;
}

[System.Diagnostics.DebuggerNonUserCode]
static int GeneratedHelperSync()
{
    return RealCode();
}

[System.Diagnostics.DebuggerNonUserCode]
async static Task<int> GeneratedHelperAsync()
{
    // F11 steps in here!
    return await Task.FromResult(RealCode());
}

private static int RealCode() 
{ 
     return 1; 
}

And I F11 (step-into) through all the statements in Visual Studio 2013. I would expect that in both calls from Main() to step me into the "return 1" statement. However, in the second case, it steps me into the async function.

It seems that the compiler erases my DebuggerNonUserCode during the async code generation.
My question is, why is it doing that? Am I missing something?

As to why am I want this, I have some helper async functions that are automatically generated and I want to avoid stepping into them all the time.

like image 480
dimio Avatar asked May 05 '14 13:05

dimio


1 Answers

This is a common "problem" with the async/await pattern. This happens to all attributes added to an async method (I had this happen with the ExcludeFromCodeCoverageAttribute and researched it a little).

What happens is, that the compiler splits the method up at the await and puts everything "after the await" into a new method, which is called when the await'ed task finishes. This new method does not get the attributes from the initial method - why I do not know.

The only way around this I know works for the ExcludeFromCodeCoverageAttribute is setting the attribute on a class level.

like image 111
Christoph Fink Avatar answered Sep 21 '22 13:09

Christoph Fink