0

I need to create a Dev Studio extension which allows issues to be create in Gitlab.

Several combo boxes are loaded and then the user hits a button and the issue is created.

The calling code looks like:

private void IssueCreator_Click(object sender, RoutedEventArgs e)
{
  var res = CreateItemsAsync(IssueName.Text, CreateLabels(), false).GetAwaiter().GetResult();
  if (res == true)
    CurrentStatus.Text = "Created Issue";
}

The actual creating code looks like:

static public async Task<bool> CreateItemsAsync(string issueName, string labels, bool createMergeRequest)
{
  if (createMergeRequest == false)
  {
    string issueURL = "https://gitlabserver.tradinghub.com/api/v4/projects/9/issues/";
    var stringContent = new FormUrlEncodedContent(new[]
    {
      new KeyValuePair<string, string>("labels", labels),
      new KeyValuePair<string, string>("title", issueName),
    });

    var response = await client.PostAsync(issueURL, stringContent);
  }
}

However, this freezes at the PostAsync call. It creates the issue but never returns.

I saw on here:

Avoiding Deadlock with HttpClient

That this type of operation is not supported in WPF.
What would be the standard way of doing this in a VSDev extension?

Stefan
  • 3,669
  • 2
  • 32
  • 43
  • There should be `var res = await CreateItemsAsync(IssueName.Text, CreateLabels(), false);` in a Click handler method that is declared `async void`. – Clemens Jan 20 '22 at 11:51
  • 1
    I'd recommend making the eventhandler `IssueCreator_Click` `async void` and replace `var res = CreateItemsAsync(IssueName.Text, CreateLabels(), false).GetAwaiter().GetResult();` by `var res = await CreateItemsAsync(IssueName.Text, CreateLabels(), false).ConfigureAwait(true);` But no guarantee that it will remove the deadlock... – lidqy Jan 20 '22 at 11:55
  • The main reason for the deadlock is the missing ConfigureAwait(false) at the end of the PostAsync statement. – Sir Rufo Jan 20 '22 at 11:57
  • @lidqy ConfigureAwait(true) is useless because it is the default behavior – Sir Rufo Jan 20 '22 at 12:00
  • https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html – Sir Rufo Jan 20 '22 at 12:02
  • @SirRufo I know it's default, but to my point of view it depends on your coding style, whether or not you want to explicitly append it. Since deadlocks might become a problem in async/await scenarios with UI, some ppl consider explicit ConfigureAwaits after each "await" good practice...Depends on one's taste jm2ct... – lidqy Jan 20 '22 at 12:09
  • 1
    @lidqy but how will you remove the deadlock with an useless statement? that is the question here, not any code styling guide. Next time we get a suggestion to put flowers on the desk, because it will look nicer … – Sir Rufo Jan 20 '22 at 12:14
  • 1
    @lidqy, thanks that got it working! – Stefan Jan 20 '22 at 15:27
  • @SirRufo The deadlock is removed by awaiting the task and copying the "context" back to the UI thread. Awaiting does the trick plus the "ConfigureAwait(true)", be it implicit or explicit. ConfigureAwait(true) is needed because the result is assigned to some controls property ("CurrentStatus.Text"). – lidqy Jan 20 '22 at 17:41

0 Answers0