| | | 1 | | using Microsoft.Extensions.Hosting; |
| | | 2 | | using Spectre.Console.Cli; |
| | | 3 | | |
| | | 4 | | namespace Orchestrator.Infrastructure; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Resolves types from the DI container for Spectre.Console.Cli. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <para> |
| | | 11 | | /// Based on the canonical implementation from spectreconsole/examples: |
| | | 12 | | /// <see href="https://github.com/spectreconsole/examples/blob/main/examples/Cli/Logging/Infrastructure/TypeResolver.cs" |
| | | 13 | | /// </para> |
| | | 14 | | /// <para> |
| | | 15 | | /// Extended to stop <see cref="IHostedService"/> instances on disposal, ensuring |
| | | 16 | | /// graceful shutdown of services started by <see cref="TypeRegistrar.Build"/>. |
| | | 17 | | /// </para> |
| | | 18 | | /// </remarks> |
| | | 19 | | public sealed class TypeResolver : ITypeResolver, IDisposable |
| | | 20 | | { |
| | | 21 | | private readonly IServiceProvider _provider; |
| | | 22 | | private readonly IReadOnlyList<IHostedService> _hostedServices; |
| | | 23 | | |
| | 1 | 24 | | public TypeResolver(IServiceProvider provider, IReadOnlyList<IHostedService> hostedServices) |
| | | 25 | | { |
| | 1 | 26 | | _provider = provider ?? throw new ArgumentNullException(nameof(provider)); |
| | 1 | 27 | | _hostedServices = hostedServices ?? throw new ArgumentNullException(nameof(hostedServices)); |
| | 1 | 28 | | } |
| | | 29 | | |
| | | 30 | | public object? Resolve(Type? type) |
| | | 31 | | { |
| | 1 | 32 | | if (type is null) |
| | | 33 | | { |
| | 1 | 34 | | return null; |
| | | 35 | | } |
| | | 36 | | |
| | 1 | 37 | | return _provider.GetService(type); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public void Dispose() |
| | | 41 | | { |
| | | 42 | | // Stop hosted services before disposing the provider to allow graceful shutdown |
| | | 43 | | // (e.g. OpenTelemetry TracerProvider flush). |
| | | 44 | | // |
| | | 45 | | // Blocking wait rationale: Spectre.Console.Cli disposes our resolver through |
| | | 46 | | // TypeResolverAdapter.Dispose(), which only checks for IDisposable — not IAsyncDisposable. |
| | | 47 | | // The enclosing `using` block in CommandExecutor.ExecuteAsync is synchronous, so there is no |
| | | 48 | | // async disposal path we can participate in. IHostedService only exposes async lifecycle |
| | | 49 | | // methods, so the .GetAwaiter().GetResult() bridge is required here. |
| | | 50 | | // See: spectre.console.cli/src/Spectre.Console.Cli/Internal/TypeResolverAdapter.cs |
| | | 51 | | // spectre.console.cli/src/Spectre.Console.Cli/Internal/CommandExecutor.cs (~line 88) |
| | 1 | 52 | | foreach (var service in _hostedServices) |
| | | 53 | | { |
| | 1 | 54 | | service.StopAsync(CancellationToken.None).GetAwaiter().GetResult(); |
| | | 55 | | } |
| | | 56 | | |
| | 1 | 57 | | if (_provider is IDisposable disposable) |
| | | 58 | | { |
| | 1 | 59 | | disposable.Dispose(); |
| | | 60 | | } |
| | 1 | 61 | | } |
| | | 62 | | } |