| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.Hosting; |
| | | 3 | | using Spectre.Console.Cli; |
| | | 4 | | |
| | | 5 | | namespace Orchestrator.Infrastructure; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Bridges Microsoft.Extensions.DependencyInjection with Spectre.Console.Cli's <see cref="ITypeRegistrar"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// <para> |
| | | 12 | | /// Based on the canonical implementation from spectreconsole/examples: |
| | | 13 | | /// <see href="https://github.com/spectreconsole/examples/blob/main/examples/Cli/Logging/Infrastructure/TypeRegistrar.cs |
| | | 14 | | /// </para> |
| | | 15 | | /// <para> |
| | | 16 | | /// Extended to start <see cref="IHostedService"/> instances after building the |
| | | 17 | | /// <see cref="ServiceProvider"/>. This is necessary because Spectre.Console.Cli does not use |
| | | 18 | | /// <c>IHost</c>, so hosted services (e.g. OpenTelemetry's <c>TelemetryHostedService</c>) |
| | | 19 | | /// would never be started otherwise. |
| | | 20 | | /// </para> |
| | | 21 | | /// </remarks> |
| | | 22 | | public sealed class TypeRegistrar : ITypeRegistrar |
| | | 23 | | { |
| | | 24 | | private readonly IServiceCollection _builder; |
| | | 25 | | |
| | 1 | 26 | | public TypeRegistrar(IServiceCollection builder) |
| | | 27 | | { |
| | 1 | 28 | | _builder = builder; |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | public ITypeResolver Build() |
| | | 32 | | { |
| | 1 | 33 | | var provider = _builder.BuildServiceProvider(); |
| | | 34 | | |
| | | 35 | | // Start any registered IHostedService instances so they can initialize |
| | | 36 | | // (e.g. OpenTelemetry builds its TracerProvider during StartAsync). |
| | | 37 | | // |
| | | 38 | | // Blocking wait rationale: IHostedService only exposes async lifecycle methods, but |
| | | 39 | | // Spectre.Console.Cli calls Build() from a synchronous context (CommandExecutor.ExecuteAsync |
| | | 40 | | // creates a synchronous `using` block around TypeResolverAdapter, which wraps our resolver). |
| | | 41 | | // TypeResolverAdapter.Dispose() only checks for IDisposable — not IAsyncDisposable — so |
| | | 42 | | // there is no async disposal path available. The .GetAwaiter().GetResult() bridge is required. |
| | | 43 | | // See: spectre.console.cli/src/Spectre.Console.Cli/Internal/TypeResolverAdapter.cs |
| | | 44 | | // spectre.console.cli/src/Spectre.Console.Cli/Internal/CommandExecutor.cs (~line 88) |
| | 1 | 45 | | var hostedServices = provider.GetServices<IHostedService>().ToList(); |
| | 1 | 46 | | foreach (var service in hostedServices) |
| | | 47 | | { |
| | 1 | 48 | | service.StartAsync(CancellationToken.None).GetAwaiter().GetResult(); |
| | | 49 | | } |
| | | 50 | | |
| | 1 | 51 | | return new TypeResolver(provider, hostedServices); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public void Register(Type service, Type implementation) |
| | | 55 | | { |
| | 1 | 56 | | _builder.AddSingleton(service, implementation); |
| | 1 | 57 | | } |
| | | 58 | | |
| | | 59 | | public void RegisterInstance(Type service, object implementation) |
| | | 60 | | { |
| | 1 | 61 | | _builder.AddSingleton(service, implementation); |
| | 1 | 62 | | } |
| | | 63 | | |
| | | 64 | | public void RegisterLazy(Type service, Func<object> func) |
| | | 65 | | { |
| | 1 | 66 | | ArgumentNullException.ThrowIfNull(func); |
| | 1 | 67 | | _builder.AddSingleton(service, _ => func()); |
| | 1 | 68 | | } |
| | | 69 | | } |