Skip to content

Support for reusing RunAsync multiple times without ServiceProvider destruction #174

@namo40

Description

@namo40

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions