dotnet / dotnet/command-line-api

Problem running async Actions

Offen
#2,152 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug
Vorherrschende Sprache
C#
Sterne
3.7k
Forks
428
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

While porting an existing project from the beta4 package in nuget to a more recent build (beta4.23181.5) I saw that async actions were not running to completion. After some debugging I found that if I made the action non-async, it would work as expected. I was able to reproduce it with the following.
```c#
using System;
using System.CommandLine;
using System.Threading.Tasks;

namespace CliAsyncTest;

public class Program
{
static async Task Main(string[] args)
{
var rootCmd = new CliRootCommand("CliAsyncTest - Test InvokeAsync()");
rootCmd.SetAction(async (_) => // Fix: remove async
{
Console.WriteLine("rootCmd Action starting");
await Task.Delay(100); // Fix: remove await, add .Wait()
Console.WriteLine("rootCmd Action complete");
});

return await rootCmd.Parse(args).InvokeAsync();
}
}
```
When I run this I will see the first line output to the console, but not the second. After applying the fixes in the comments I will see both lines output.

Tested with:
- beta4.23181.5
- Windows 10

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit der bereitgestellten C#-Reproduktion und konzentriere dich auf SetAction und den Einstiegspunkt Parse(args).InvokeAsync(). Führe sie mit der gemeldeten Paketversion aus und vergleiche asynchrone und synchrone Aktionen. Fertig ist es, wenn die asynchrone Aktion sowohl ihre Start- als auch ihre Abschlussmeldung ausgibt, bevor InvokeAsync zurückkehrt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp
Bereich
cli
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.