Skip to content

Commit e2b8e7d

Browse files
authored
Update ReadMe.md
1 parent 907e106 commit e2b8e7d

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

ReadMe.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,22 @@ Prevent ServiceProvider auto dispose
14691469
When executing commands with `Run/RunAsync`, the ServiceProvider is automatically disposed. This becomes a problem when executing commands multiple times or when you want to use the ServiceProvider after the command finishes. In Run/RunAsync from ConsoleAppBuilder, you can stop the automatic disposal of ServiceProvider by setting `bool disposeServiceProvider` to `false`.
14701470

14711471
```csharp
1472-
var app = ConsoleApp.Create();
1473-
await app.RunAsync(args, disposeServiceProvider: false); // default is true
1472+
try
1473+
{
1474+
// If ConsoleAppFramework throws error/canceled, ExitCode will be set to non-zero.
1475+
while (Environment.ExitCode == 0)
1476+
{
1477+
var command = Console.ReadLine();
1478+
if (command == null) break;
1479+
1480+
await app.RunAsync(command.Split(' '), disposeServiceProvider: false);
1481+
}
1482+
}
1483+
finally
1484+
{
1485+
// finally dispose ServiceProvider
1486+
(ConsoleApp.ServiceProvider as IDisposable)?.Dispose();
1487+
}
14741488
```
14751489

14761490
When `Microsoft.Extensions.Hosting` is referenced, `bool startHost, bool stopHost, bool disposeServiceProvider` become controllable. The defaults are all `true`.

0 commit comments

Comments
 (0)