-
Notifications
You must be signed in to change notification settings - Fork 113
Closed
Description
In version 5.2.4, it was possible to call RunAsync multiple times because the ServiceProvider remained alive.
However, in later versions, RunAsync now immediately destroys the ServiceProvider after execution, making it impossible to reuse.
var isRunning = true;
app.Add("exit", () => isRunning = false);
while (isRunning)
{
Console.Write("Cli> ");
var readLine = Console.ReadLine();
if (string.IsNullOrEmpty(readLine)
continue;
var parameters = readLine.Split(" ");
await RunAsync(parameters);
}
I would like to use RunAsync directly in a loop as shown in the first example, like in version 5.2.4
And currently, we are temporarily using the code written below
namespace ConsoleAppFramework;
internal static partial class ConsoleApp
{
internal partial class ConsoleAppBuilder
{
internal class Disposable(Func<Task> disposable) : IAsyncDisposable
{
public async ValueTask DisposeAsync()
{
await disposable.Invoke();
}
}
internal IAsyncDisposable Build()
{
BuildAndSetServiceProvider();
return new Disposable(async () =>
{
if (ServiceProvider is IAsyncDisposable ad)
{
await ad.DisposeAsync();
}
else if (ServiceProvider is IDisposable d)
{
d.Dispose();
}
});
}
internal async Task RunCommandAsync(string[] args)
{
try
{
Task task = null;
RunAsyncCore(args, ref task!);
if (task != null)
{
await task;
}
}
finally
{
}
}
}
}
var isRunning = true;
app.Add("exit", () => isRunning = false);
await using var _ = app.Build();
while (isRunning)
{
Console.Write("Cli> ");
var readLine = Console.ReadLine();
if (string.IsNullOrEmpty(readLine)
continue;
var parameters = readLine.Split(" ");
await app.RunCommandAsync(parameters);
}
// Dummy
await app.RunAsync(["exit"]);
Could you please provide support to allow reuse like in version 5.2.4?
RobertK66 and filzrev
Metadata
Metadata
Assignees
Labels
No labels