dotnet / dotnet/command-line-api

Problem running async Actions

Aperta
#2,152 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
C#
Stelle
3.7k
Fork
428
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.