public ServiceProvider BuildServiceProvider() => new ServiceCollection() .AddTransient(_ => _config.GetSection("PluralKit").Get <CoreConfig>() ?? new CoreConfig()) .AddTransient(_ => _config.GetSection("PluralKit").GetSection("Bot").Get <BotConfig>() ?? new BotConfig()) .AddSingleton <DbConnectionCountHolder>() .AddTransient <DbConnectionFactory>() .AddSingleton <IDiscordClient, DiscordShardedClient>(_ => new DiscordShardedClient(new DiscordSocketConfig { MessageCacheSize = 5, ExclusiveBulkDelete = true })) .AddSingleton <Bot>() .AddSingleton(_ => new CommandService(new CommandServiceConfig { CaseSensitiveCommands = false, QuotationMarkAliasMap = new Dictionary <char, char> { { '"', '"' }, { '\'', '\'' }, { '‘', '’' }, { '“', '”' }, { '„', '‟' }, }, // We're already asyncing stuff by forking off at the client event handlers // So adding an additional layer of forking is pointless // and leads to the service scope being disposed of prematurely DefaultRunMode = RunMode.Sync })) .AddTransient <EmbedService>() .AddTransient <ProxyService>() .AddTransient <LogChannelService>() .AddTransient <DataFileService>() .AddTransient <WebhookExecutorService>() .AddTransient <ProxyCacheService>() .AddSingleton <WebhookCacheService>() .AddTransient <SystemStore>() .AddTransient <MemberStore>() .AddTransient <MessageStore>() .AddTransient <SwitchStore>() .AddSingleton(svc => InitUtils.InitMetrics(svc.GetRequiredService <CoreConfig>())) .AddSingleton <PeriodicStatCollector>() .AddScoped(_ => new Sentry.Scope(null)) .AddTransient <PKEventHandler>() .AddScoped <EventIdProvider>() .AddSingleton(svc => new LoggerProvider(svc.GetRequiredService <CoreConfig>(), "bot")) .AddScoped(svc => svc.GetRequiredService <LoggerProvider>().RootLogger.ForContext("EventId", svc.GetRequiredService <EventIdProvider>().EventId)) .AddMemoryCache() .BuildServiceProvider();
public ServiceProvider BuildServiceProvider() => new ServiceCollection() .AddTransient(_ => _config.GetSection("PluralKit").Get <CoreConfig>() ?? new CoreConfig()) .AddTransient(_ => _config.GetSection("PluralKit").GetSection("Bot").Get <BotConfig>() ?? new BotConfig()) .AddSingleton <DbConnectionCountHolder>() .AddTransient <DbConnectionFactory>() .AddSingleton <IDiscordClient, DiscordShardedClient>(_ => new DiscordShardedClient(new DiscordSocketConfig { MessageCacheSize = 5, ExclusiveBulkDelete = true, DefaultRetryMode = RetryMode.AlwaysRetry, // Commented this out since Debug actually sends, uh, quite a lot that's not necessary in production // but leaving it here in case I (or someone else) get[s] confused about why logging isn't working again :p // LogLevel = LogSeverity.Debug // We filter log levels in Serilog, so just pass everything through (Debug is lower than Verbose) })) .AddSingleton <Bot>() .AddTransient <CommandTree>() .AddTransient <SystemCommands>() .AddTransient <MemberCommands>() .AddTransient <SwitchCommands>() .AddTransient <LinkCommands>() .AddTransient <APICommands>() .AddTransient <ImportExportCommands>() .AddTransient <HelpCommands>() .AddTransient <ModCommands>() .AddTransient <MiscCommands>() .AddTransient <EmbedService>() .AddTransient <ProxyService>() .AddTransient <LogChannelService>() .AddTransient <DataFileService>() .AddTransient <WebhookExecutorService>() .AddTransient <ProxyCacheService>() .AddSingleton <WebhookCacheService>() .AddTransient <IDataStore, PostgresDataStore>() .AddSingleton(svc => InitUtils.InitMetrics(svc.GetRequiredService <CoreConfig>())) .AddSingleton <PeriodicStatCollector>() .AddScoped(_ => new Sentry.Scope(null)) .AddTransient <PKEventHandler>() .AddScoped <EventIdProvider>() .AddSingleton(svc => new LoggerProvider(svc.GetRequiredService <CoreConfig>(), "bot")) .AddScoped(svc => svc.GetRequiredService <LoggerProvider>().RootLogger.ForContext("EventId", svc.GetRequiredService <EventIdProvider>().EventId)) .AddMemoryCache() .BuildServiceProvider();
public ServiceProvider BuildServiceProvider() => new ServiceCollection() .AddTransient(_ => _config.GetSection("PluralKit").Get <CoreConfig>() ?? new CoreConfig()) .AddTransient(_ => _config.GetSection("PluralKit").GetSection("Bot").Get <BotConfig>() ?? new BotConfig()) .AddSingleton <DbConnectionCountHolder>() .AddTransient <DbConnectionFactory>() .AddSingleton <IDiscordClient, DiscordShardedClient>(_ => new DiscordShardedClient(new DiscordSocketConfig { MessageCacheSize = 5, ExclusiveBulkDelete = true, DefaultRetryMode = RetryMode.AlwaysRetry })) .AddSingleton <Bot>() .AddTransient <CommandTree>() .AddTransient <SystemCommands>() .AddTransient <MemberCommands>() .AddTransient <SwitchCommands>() .AddTransient <LinkCommands>() .AddTransient <APICommands>() .AddTransient <ImportExportCommands>() .AddTransient <HelpCommands>() .AddTransient <ModCommands>() .AddTransient <MiscCommands>() .AddTransient <EmbedService>() .AddTransient <ProxyService>() .AddTransient <LogChannelService>() .AddTransient <DataFileService>() .AddTransient <WebhookExecutorService>() .AddTransient <ProxyCacheService>() .AddSingleton <WebhookCacheService>() .AddTransient <SystemStore>() .AddTransient <MemberStore>() .AddTransient <MessageStore>() .AddTransient <SwitchStore>() .AddSingleton(svc => InitUtils.InitMetrics(svc.GetRequiredService <CoreConfig>())) .AddSingleton <PeriodicStatCollector>() .AddScoped(_ => new Sentry.Scope(null)) .AddTransient <PKEventHandler>() .AddScoped <EventIdProvider>() .AddSingleton(svc => new LoggerProvider(svc.GetRequiredService <CoreConfig>(), "bot")) .AddScoped(svc => svc.GetRequiredService <LoggerProvider>().RootLogger.ForContext("EventId", svc.GetRequiredService <EventIdProvider>().EventId)) .AddMemoryCache() .BuildServiceProvider();
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddCors(); services.AddMvc(opts => { }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .AddJsonOptions(opts => { opts.SerializerSettings.BuildSerializerSettings(); }); services .AddTransient <IDataStore, PostgresDataStore>() .AddSingleton(svc => InitUtils.InitMetrics(svc.GetRequiredService <CoreConfig>(), "API")) .AddScoped <TokenAuthService>() .AddTransient(_ => Configuration.GetSection("PluralKit").Get <CoreConfig>() ?? new CoreConfig()) .AddSingleton(svc => InitUtils.InitLogger(svc.GetRequiredService <CoreConfig>(), "api")) .AddTransient <DbConnectionCountHolder>() .AddTransient <DbConnectionFactory>(); }