/// <summary> /// Initializes the <see cref="Startup"/> class. /// </summary> /// <param name="configuration">The configuration.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <exception cref="ArgumentNullException">If configuration is null.</exception> /// <returns>Returns an awaitable <see cref="Task"/>.</returns> internal Task InitializeAsync(IConfiguration configuration, CancellationToken cancellationToken) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } _serviceProvider = new ServiceCollection() .AddConfigurations(configuration) .AddApplicationServices() .AddJwtBearerBasedAuthentication() .BuildServiceProvider() .ToGenericServiceProvider(); List <Task> tasks = new List <Task> { Task.Run(() => VerifyDatabaseConnection(_cancellationTokenSourceTimed.Token), cancellationToken), Task.Run(() => MapMessagesToServices(_cancellationTokenSourceTimed.Token), cancellationToken), Task.Run(() => CreateServerCertificate(_cancellationTokenSourceTimed.Token), cancellationToken) }; return(Task.WhenAll(tasks).ContinueWith(task => { if (task.IsCanceled) { _cancellationTokenSource.Cancel(); } }, cancellationToken)); }
/// <summary> /// Initializes the <see cref="Startup"/> instance. /// </summary> /// <param name="configuration">The configuration.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <exception cref="ArgumentNullException">Thrown when the configuration is null.</exception> /// <returns>Returns an awaitable <see cref="Task"/>.</returns> public Task InitializeAsync(IConfiguration configuration, CancellationToken cancellationToken) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } _genericServiceProvider = new ServiceCollection() .AddConfigurations(configuration) .Configure <MessageQueueConfiguration>(configuration.GetSection("MessageQueue").Bind) .Configure <RegistryConfiguration>(configuration.GetSection("Registry").Bind) .AddApplicationServices() .BuildServiceProvider() .ToGenericServiceProvider(); List <Task> tasks = new List <Task> { Task.Run(() => CreateServerCertificate(_cancellationTokenSourceTimed.Token), cancellationToken), // Initiate service map.. Task.Run(_genericServiceProvider.GetService <IMessageToServiceMapper>, cancellationToken) }; return(Task.WhenAll(tasks).ContinueWith(task => { if (task.IsCanceled) { _cancellationTokenSource.Cancel(); } }, cancellationToken)); }
/// <summary> /// Initializes the <see cref="Startup"/> instance. /// </summary> /// <param name="configuration">The configuration.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <exception cref="ArgumentNullException">Thrown when the configuration is null.</exception> /// <returns>Returns an awaitable <see cref="Task"/>.</returns> public Task InitializeAsync(IConfiguration configuration, CancellationToken cancellationToken) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } _genericServiceProvider = new ServiceCollection() .AddApplicationServices(configuration) .BuildServiceProvider() .ToGenericServiceProvider(); // NOTE: Start up the command handler first! _genericServiceProvider.GetRequiredService <ICommandHandler>(); List <Task> tasks = new List <Task> { Task.Run(_genericServiceProvider.GetService <IDiscordStartupService>().StartAsync, cancellationToken) // TODO: Add tasks for initialization. }; return(Task.WhenAll(tasks).ContinueWith(task => { if (task.IsCanceled) { _cancellationTokenSource.Cancel(); } }, cancellationToken)); }
/// <summary> /// Runs the program asynchronously. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Returns an awaitable <see cref="Task"/>.</returns> private async Task RunAsync(CancellationToken cancellationToken) { IConfiguration configuration; try { IConfigurationBuilder builder = new ConfigurationBuilder() .SetBasePath(Environment.CurrentDirectory) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); configuration = builder.Build(); } catch (Exception) { Console.WriteLine("Please check if you have a valid appsettings.json!"); CancellationTokenSource.Cancel(); return; } Startup startup = new Startup(CancellationTokenSource, 60); Console.WriteLine("Initializing..."); await startup.InitializeAsync(configuration, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); Console.WriteLine("Finished initializing!\n"); _genericServiceProvider = startup.GetGenericServiceProvider(); // TODO: Start whatever services we need. }
internal Task InitializeAsync() { _serviceProvider = new ServiceCollection() .AddApplicationServices() .BuildServiceProvider() .ToGenericServiceProvider(); return(Task.CompletedTask); }
public static async Task Main(string[] args) { Startup startup = new Startup(); await startup.InitializeAsync(); IGenericServiceProvider serviceProvider = startup.GetServiceProvider(); IPuzzleService <SudokuPuzzle> puzzleService = serviceProvider.GetService <IPuzzleService <SudokuPuzzle> >(); SudokuPuzzle puzzle = puzzleService.Generate(); string beforePuzzle = puzzleService.Print(puzzle); Console.WriteLine("Sudoku"); Console.Write(beforePuzzle); Stopwatch stopwatch = Stopwatch.StartNew(); PuzzleSolveDto puzzleSolveDto = puzzleService.Solve(puzzle); stopwatch.Stop(); if (puzzleSolveDto.Success) { Console.WriteLine($"Solved in: {stopwatch.ElapsedMilliseconds}ms"); string afterPuzzle = puzzleService.Print(puzzle); Console.WriteLine(afterPuzzle); ConsoleKeyInfo input; do { Console.Write("\rPrint all puzzle states? [y/n]\n"); input = Console.ReadKey(); if (input.KeyChar == 'y') { foreach (PuzzleState state in puzzleSolveDto.PuzzleStates) { Console.Write("\n" + state); } Console.WriteLine(); break; } if (!input.KeyChar.Equals('n')) { continue; } Console.WriteLine(); break; } while (!input.KeyChar.Equals('y') || !input.KeyChar.Equals('n')); } else { Console.WriteLine("Failed to solve puzzle!"); } Console.WriteLine("Done. Press any key to quit."); Console.ReadKey(); }
/// <summary> /// Runs the <see cref="Program"/> asynchronously. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Returns an awaitable <see cref="Task"/>.</returns> private async Task RunAsync(CancellationToken cancellationToken) { IConfiguration configuration; try { configuration = ConfigurationLoader.GetConfiguration("appSettings"); } catch (Exception) { Console.WriteLine("Please check if you have a valid appSettings.json!"); CancellationTokenSource.Cancel(); return; } Startup startup = new Startup(CancellationTokenSource); Console.WriteLine("Initializing..."); await startup.InitializeAsync(configuration, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); Console.WriteLine("Finished initializing!\n"); Interlocked.Exchange(ref _canReadConsole, 1); Interlocked.Exchange(ref _canExit, 1); _genericServiceProvider = startup.GetGenericServiceProvider(); GetEnabledTrainingRoomsResponse getEnabledTrainingRoomsResponse = await _genericServiceProvider.GetService <ITrainingRoomService>() .GetEnabledTrainingRoomsAsync(new GetEnabledTrainingRoomsRequest()); foreach (TrainingRoomDto trainingRoomDto in getEnabledTrainingRoomsResponse.TrainingRooms) { Console.WriteLine($"TrainingRoom:\n\tId: {trainingRoomDto.Id}\n\tName: {trainingRoomDto.Name}\n\tOwner: {trainingRoomDto.Owner.Username}"); } ServerConfiguration serverConfiguration = _genericServiceProvider.GetService <IOptions <ServerConfiguration> >().Value; TcpListener tcpListener = new TcpListener(IPAddress.Any, serverConfiguration.Port); tcpListener.Start(); Console.WriteLine($"Started listening for clients on port: {serverConfiguration.Port}."); while (!cancellationToken.IsCancellationRequested) { TcpClient tcpClient = await tcpListener.AcceptTcpClientAsync(); Console.WriteLine($"Accepted a new connection: \n\tLocalEndPoint: {tcpClient.Client.LocalEndPoint}\n\tRemoteEndPoint: {tcpClient.Client.RemoteEndPoint}"); _ = Task.Run(async() => { IMessageProcessor messageProcessor = new ServerMessageProcessor(_genericServiceProvider.GetService <MessageToServiceMapper>()); IMessageSerializer messageSerializer = new JsonMessageSerializer(); SslTcpNetworkConnector networkConnector = new SslTcpNetworkConnector(messageSerializer, messageProcessor, tcpClient); await networkConnector.AuthenticateAsServer(serverConfiguration.Certificate, CancellationToken.None); networkConnector.Start(); }, cancellationToken); } }
public static IKernel InjectServices(this IKernel kernel, IGenericServiceProvider sp, IGenericServiceProvider gsp) { // handlers kernel.Bind <IProErrorHandler>().To <ProErrorHandler>(); // command services var oleMenuCommandService = sp.GetService <IMenuCommandService, OleMenuCommandService>(); kernel.Bind <IMenuCommandService>().ToConstant(oleMenuCommandService); kernel.Bind <IOleCommandTarget>().ToConstant(oleMenuCommandService); kernel.Bind <IProCommandService>().To <ProCommandService>(); // global services kernel.Bind <IVsMonitorSelection>().ToMethod(ctx => gsp.GetService <SVsShellMonitorSelection, IVsMonitorSelection>()); kernel.Bind <IVsSolution>().ToMethod(ctx => gsp.GetService <SVsSolution, IVsSolution>()); return(kernel); }
/// <summary> /// Runs the program asynchronously. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Returns an awaitable <see cref="Task"/>.</returns> private async Task RunAsync(CancellationToken cancellationToken) { IConfiguration configuration; try { configuration = ConfigurationLoader.GetConfiguration("appsettings"); } catch (Exception e) { Console.WriteLine("Please check if you have a valid appsettings.json!"); Console.WriteLine(e.Message); CancellationTokenSource.Cancel(); return; } Startup startup = new Startup(CancellationTokenSource, 60); Console.WriteLine("Initializing..."); await startup.InitializeAsync(configuration, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); Console.WriteLine("Finished initializing!"); IGenericServiceProvider genericServiceProvider = startup.GetGenericServiceProvider(); IRegistryService registryService = genericServiceProvider.GetService <IRegistryService>(); _ = Task.Run(async() => await registryService.StartReceivingServiceEndPointsAsync(cancellationToken), cancellationToken); Console.WriteLine("Started RegistryService EndPoint."); _ = Task.Run(async() => await registryService.StartMonitoringServicesAsync(cancellationToken), cancellationToken); Console.WriteLine("Started monitoring services."); IClientMessageProcessor clientMessageProcessor = genericServiceProvider.GetService <IClientMessageProcessor>(); _ = Task.Run(async() => await clientMessageProcessor.StartAsync(cancellationToken), cancellationToken); Console.WriteLine("Started client messaging EndPoint."); Interlocked.Exchange(ref _canReadConsole, 1); Interlocked.Exchange(ref _canExit, 1); }
/// <summary> /// Runs the <see cref="Program"/> asynchronously. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Returns an awaitable <see cref="Task"/>.</returns> private async Task RunAsync(CancellationToken cancellationToken) { IConfiguration configuration; try { configuration = ConfigurationLoader.GetConfiguration("appSettings"); } catch (Exception) { Console.WriteLine("Please check if you have a valid appSettings.json!"); CancellationTokenSource.Cancel(); return; } Startup startup = new Startup(CancellationTokenSource, 60); Console.WriteLine("Initializing..."); await startup.InitializeAsync(configuration, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); Console.WriteLine("Finished initializing!\n"); _genericServiceProvider = startup.GetGenericServiceProvider(); GetEnabledTrainingRoomsResponse getEnabledTrainingRoomsResponse = await _genericServiceProvider.GetService <ITrainingRoomService>() .GetEnabledTrainingRoomsAsync(new GetEnabledTrainingRoomsRequest()); foreach (TrainingRoomDto trainingRoomDto in getEnabledTrainingRoomsResponse.TrainingRooms) { Console.WriteLine($"TrainingRoom:\n\tId: {trainingRoomDto.Id}\n\tName: {trainingRoomDto.Name}\n\tOwner: {trainingRoomDto.Owner?.Username}"); } MessageToServiceMapper messageToServiceMapper = _genericServiceProvider.GetService <MessageToServiceMapper>(); IMessageSerializer messageSerializer = _genericServiceProvider.GetService <IMessageSerializer>(); IMessageProcessor messageProcessor = new ServerMessageProcessor(messageToServiceMapper); List <Task> tasks = new List <Task>(); Console.WriteLine("Would you like to start the ClientEndPoint? y/n"); ConsoleKeyInfo keyInfo = await WaitForReadKey(cancellationToken); bool runClientEndPoint = keyInfo.KeyChar == 'y'; if (runClientEndPoint) { tasks.Add(Task.Run(() => RunClientEndPoint(cancellationToken, messageProcessor, messageSerializer), cancellationToken)); } Console.WriteLine(); Console.WriteLine("Would you like to start the RestEndPoint? y/n"); keyInfo = await WaitForReadKey(cancellationToken); bool runRestEndPoint = keyInfo.KeyChar == 'y'; if (runRestEndPoint) { tasks.Add(Task.Run(() => RunRestEndPoint(cancellationToken, messageProcessor, messageSerializer), cancellationToken)); } Console.WriteLine(); Interlocked.Exchange(ref _canReadConsole, 1); Interlocked.Exchange(ref _canExit, 1); await Task.WhenAll(tasks).ContinueWith(task => { if (task.IsCanceled) { CancellationTokenSource.Cancel(); } }, cancellationToken); }
public SubscriberTests(SubscriberFixture subscriberFixture) { _serviceProvider = subscriberFixture.ServiceProvider; _integrationTest = subscriberFixture.IntegrationTest; _appVeyorCIBuild = subscriberFixture.AppVeyorCIBuild; }
public TestMessageSender(IGenericServiceProvider serviceProvider, bool integrationTest = false) { _serviceProvider = serviceProvider; _integrationTest = integrationTest; }
public CustomerQueryService(IGenericServiceProvider genericServiceProvider) { _genericServiceProvider = genericServiceProvider; }
public CustomerPropertyService(IGenericServiceProvider genericServiceProvider) { _genericServiceProvider = genericServiceProvider; }
public CustomerService(IGenericServiceProvider serviceProvider) { _serviceProvider = serviceProvider; }