Exemple #1
0
        private async Task Start(string[] args)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddInterfacesAndSelfAsSingleton(this);
            serviceCollection.AddInterfacesAndSelfAsSingleton <FilePathService>();
            serviceCollection.AddInterfacesAndSelfAsSingleton <LoggerService>();
            serviceCollection.AddInterfacesAndSelfAsSingleton <BootstrapService>();
            serviceCollection.AddInterfacesAndSelfAsSingleton <ConfigService>();
            serviceCollection.AddInterfacesAndSelfAsSingleton <SqliteDatabaseService>();
            serviceCollection.AddInterfacesAndSelfAsSingleton <RpcService>();
            serviceCollection.AddInterfacesAndSelfAsSingleton <DiscordAppService>();
            serviceCollection.AddInterfacesAndSelfAsSingleton <ConsoleCommandService>();

            ServiceProvider = serviceCollection.BuildServiceProvider();

            CommandService = new CommandService(new CommandServiceConfig {
                CaseSensitiveCommands = false
            });
            await CommandService.AddModulesAsync(Assembly.GetExecutingAssembly(), ServiceProvider).ConfigureAwait(false);

            CancellationTokenSource = new CancellationTokenSource();
            TasksToAwait            = new List <Task>();

            try
            {
                ServiceProvider.InitializeServices();
                ServiceProvider.LateInitializeServices();

                Task.WaitAll(TasksToAwait.ToArray());

                ServiceProvider.DisposeServices();
            }
            catch (AggregateException ex)
            {
                var loggerService = ServiceProvider?.GetService <LoggerService>();

                if (loggerService != null)
                {
                    foreach (var exception in ex.InnerExceptions)
                    {
                        if (exception is TaskCanceledException)
                        {
                            continue;
                        }

                        await loggerService.LogErrorMessageAsync(exception).ConfigureAwait(false);
                    }
                }

                CancellationTokenSource?.Cancel();
                ServiceProvider?.DisposeServices();
                Environment.Exit(1);
                return;
            }
            catch (Exception ex)
            {
                var loggerService = ServiceProvider?.GetService <LoggerService>();

                if (loggerService != null)
                {
                    await loggerService.LogErrorMessageAsync(ex).ConfigureAwait(false);
                }

                CancellationTokenSource?.Cancel();
                ServiceProvider?.DisposeServices();
                Environment.Exit(1);
                return;
            }

            Environment.Exit(0);
        }