What does ‘context’ exactly mean in C# async/await code?

If I’m not totally wrong, ConfigureAwait(false); only means that the code which runs after the code you are awaiting, is not required to use the SynchronizationContext from before the await.

SynchronizationContext can be different things, as Stephen pointed out. Imagine you are in a web environment and your code after the await relies on HttpContext.Current.Items: this might not work anymore if you set ConfigureAwait(false);.

The following code in an MVC controller would throw an exception, for example:

public async Task<ActionResult> Index()
{
    System.Web.HttpContext.Current.Items["test"] = "test";

    var result = await SomethingAsync();
            
    return View();
}

private async Task<object> SomethingAsync()
{
    await Task.Delay(1000).ConfigureAwait(false);

    // this will throw a nullpointer if ConfigureAwait is set to false
    return System.Web.HttpContext.Current.Items["test"];
}

Your variable though is simply in the scope of the method and therefor it will be available, basically the method closure/scope, if this makes sense?

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)