Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            AddSingletonFromFile <AppSettings>(services, Configuration.GetSection("AppSettings"));
            services.AddSingleton <TelegramBot>(serviceProvider =>
            {
                var appSettings    = serviceProvider.GetService <AppSettings>();
                var telegramBotKey = appSettings.TelegramBotKey;
                WebProxy proxy     = null;
                if (!string.IsNullOrEmpty(appSettings.TelegramProxyAddress) && appSettings.TelegramProxyPort.HasValue)
                {
                    proxy = new WebProxy(appSettings.TelegramProxyAddress, appSettings.TelegramProxyPort.Value);
                    proxy.BypassProxyOnLocal = false;
                }
                var bot = new TelegramBot(telegramBotKey, serviceProvider.GetRequiredService <TelegramBotMessageHandler>(), proxy);
                bot.Activate();
                return(bot);
            });
            services.AddTransient <AuthorizationService>();
            services.AddTransient <TelegramBotMessageHandler>();
            services.AddSingleton <CrmClientService>();
            services.AddTransient <CrmService>();
            services.AddTransient <ConversationService>();
            services.AddTransient(typeof(Lazy <>), typeof(LazyService <>));
            AddCommands(services);

            services.AddDataProtection();
            services.AddMemoryCache();
            services.AddMvc(); // Fix compilation error if referencing NetStandard2.0 assemblies

            string appInsightsKey = Configuration.GetSection("ApplicationInsights")["InstrumentationKey"];

            if (!string.IsNullOrEmpty(appInsightsKey))
            {
                services.AddApplicationInsightsTelemetry(appInsightsKey);
            }
            services.AddScoped <TelemetryClient>();

            string connectionString = Configuration["connectionString"];

            services.AddDbContext <DatabaseContext>(options => options.UseSqlServer(connectionString), ServiceLifetime.Singleton);
            services.AddSingleton <IAppUnitOfWorkFactory, AppUnitOfWorkFactory>(serviceProvider =>
            {
                var options = serviceProvider.GetService <DbContextOptions <DatabaseContext> >();
                return(new AppUnitOfWorkFactory(options));
            });
            services.AddTransient <TelegramChatService>();
            services.AddTransient <UserService>();
            services.AddTransient <NotificationSubscriptionService>();

            // Register scheduled tasks.
            services.AddSingleton <IScheduledTask, CheckSubmittedDailyReportsTask>();
            services.AddScheduler((sender, args) =>
            {
                new TelemetryClient().TrackException(args.Exception);
                args.SetObserved();
            });
        }