private IServiceProvider Build(string[] args, Type[] commandTypes, Action <CoconaLiteAppOptions>?configureOptions) { var services = new CoconaLiteServiceCollection(); var options = new CoconaLiteAppOptions() { CommandTypes = commandTypes, }; configureOptions?.Invoke(options); services.AddSingleton(options); services.AddCoconaCore(args); services.AddCoconaShellCompletion(); _configureServicesDelegate?.Invoke(services); var serviceProvider = new CoconaLiteServiceProvider(services); serviceProvider.GetService <ICoconaCommandDispatcherPipelineBuilder>() .UseMiddleware <HandleExceptionAndExitMiddleware>() .UseMiddleware <HandleParameterBindExceptionMiddleware>() .UseMiddleware <RejectUnknownOptionsMiddleware>() .UseMiddleware <CommandFilterMiddleware>() .UseMiddleware((next, sp) => new InitializeCoconaLiteConsoleAppMiddleware(next, sp.GetService <ICoconaAppContextAccessor>())) .UseMiddleware <CoconaCommandInvokeMiddleware>(); return(serviceProvider); }
public void AddSingleton_Type() { var services = new CoconaLiteServiceCollection(); services.AddSingleton <IMyService, MyService>(); services.Should().HaveCount(1); services.Should().Contain(x => x.ServiceType == typeof(IMyService) && x.Factory != null); }
public void HasDependency_Singleton_TService_TImplementation() { var services = new CoconaLiteServiceCollection(); services.AddSingleton <IMyService, MyService>(); services.AddSingleton <IYetAnotherService, YetAnotherService>(); var serviceProvider = new CoconaLiteServiceProvider(services); var instance = serviceProvider.GetService <IYetAnotherService>(); instance.Should().NotBeNull(); instance.Should().BeOfType <YetAnotherService>(); ((YetAnotherService)instance).MyService.Should().NotBeNull(); var instanceOfMyService = serviceProvider.GetService <IMyService>(); ((YetAnotherService)instance).MyService.Should().BeSameAs(instanceOfMyService); serviceProvider.Dispose(); ((YetAnotherService)instance).IsDisposed.Should().BeTrue(); ((MyService)((YetAnotherService)instance).MyService).IsDisposed.Should().BeTrue(); }
public void NoDependency_Singleton_TService_Factory() { var services = new CoconaLiteServiceCollection(); services.AddSingleton <IMyService>(_ => new MyService()); var serviceProvider = new CoconaLiteServiceProvider(services); var instance = serviceProvider.GetService <IMyService>(); instance.Should().NotBeNull(); instance.Should().BeOfType <MyService>(); var instance2 = serviceProvider.GetService <IMyService>(); instance.Should().BeSameAs(instance2); instance.Id.Should().Be(instance2.Id); serviceProvider.Dispose(); ((MyService)instance).IsDisposed.Should().BeTrue(); }