示例#1
0
        public void Start()
        {
            try {
                _services.AddSingleton <IPollerHistoryStorage>(new TempPollerHistoryStorage(0));
                _services.AddSingleton <ITelegramClient, TelegramClient>();
                _services.AddSingleton <IClientConfiguration, ClientAutoConfiguration>();
                _services.AddSingleton <IAutoPollerConfiguration, AutoPollerAutoConfiguration>();
                _services.AddSingleton <IMiddlewaresChainFactory, MiddlewaresChainFactory>();

                _services.AddSingleton <LocalizationManager>();
                _services.AddSingleton <ILocalizationProvider, EnglishLocalizationProvider>();

                var middlewaresChainBuilder = new MiddlewaresChainBuilder();
                middlewaresChainBuilder.InsertFirst <HandleErrorMiddleware>();
                _services.AddSingleton(middlewaresChainBuilder);

                _services.AddControllers(b =>
                                         b.SetAssembly(GetType().GetTypeInfo().Assembly)
                                         .GenerateRoutes()
                                         );

                ConfigureServices(_services);

                _serviceProvider = CreateServiceProvider(_services);

                // check all required services
                _serviceProvider.GetRequiredService <IRouter>();
                _serviceProvider.GetRequiredService <IControllerFactory>();
                _client = _serviceProvider.GetRequiredService <ITelegramClient>();

                var loggerFactory = _serviceProvider.GetRequiredService <ILoggerFactory>();
                _logger = loggerFactory.CreateLogger(GetType());

                var autoPollerConfig = _serviceProvider.GetService <IAutoPollerConfiguration>();
                if ((autoPollerConfig as AutoPollerAutoConfiguration)?.Enabled ?? true)
                {
                    _autoPoller = _serviceProvider.GetInstance <AutoPoller>();
                    _autoPoller.UpdateReceived += (sender, args) => EnqueueUpdate(args.Update);
                    _autoPoller.Start();
                    _logger.LogInformation("Auto polling has been enabled...");
                }

                CallConfigure();

                var middlewaresChainFactory = _serviceProvider.GetRequiredService <IMiddlewaresChainFactory>();
                _middlewaresChain        = middlewaresChainBuilder.Build(middlewaresChainFactory);
                _cancellationTokenSource = new CancellationTokenSource();
                _updatesProcessingTask   = ProcessUpdates(_cancellationTokenSource.Token);

                _logger.LogInformation("Application started...");
            }
            catch (Exception e) {
                Console.WriteLine("Cannot start application: " + e.ToString());
                throw;
            }
        }
 public AutoPollingBotApplication(
     IMiddlewaresChain middlewares,
     IAutoPollerConfiguration autoPollerConfiguration,
     AutoPoller autoPoller,
     ILogger <AutoPollingBotApplication> logger
     ) : base(middlewares, logger)
 {
     _autoPollerConfiguration = autoPollerConfiguration;
     _autoPoller = autoPoller;
     _autoPoller.UpdateReceived += (sender, eventArgs) => EnqueueUpdate(eventArgs.Update);
 }