Exemple #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMemoryCache(options => {
                options.SizeLimit = this.Configuration.GetValue <long?>("MemCache:SizeLimit", null);
            });

            ILyricsGenerator lyricsGenerator =
                this.Configuration.GetValue <string>("Generator", null) == "dummy"
                ? new DummyLyrics()
                : this.CreateGradientLyrics();

            CheckGeneratorSanity(lyricsGenerator);
            services.AddSingleton(lyricsGenerator);

            services.Configure <IdentityOptions>(options => {
                options.Password.RequiredUniqueChars    = 4;
                options.Password.RequiredLength         = 5;
                options.Password.RequireDigit           = false;
                options.Password.RequireLowercase       = options.Password.RequireUppercase = false;
                options.Password.RequireNonAlphanumeric = false;
            });

            services.Configure <CookiePolicyOptions>(options => {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext <ApplicationDbContext>(this.ConfigureDbContext);
            services.AddDefaultIdentity <SongsUser>()
            .AddDefaultUI()
            .AddEntityFrameworkStores <ApplicationDbContext>();

            services.AddScoped <ISongDatabase, CachedSongDatabase>();

            services.AddSingleton(sp => {
                var scopedProvider = sp.CreateScope().ServiceProvider;
                return(SongVoteCache.Load(scopedProvider.GetService <ApplicationDbContext>().Votes));
            });
            services.AddSingleton(sp => {
                var scopedProvider = sp.CreateScope().ServiceProvider;
                return(new PregeneratedSongProvider(
                           scopedProvider.GetService <ISongDatabase>(),
                           scopedProvider.GetService <ApplicationDbContext>().Songs,
                           sp.GetService <ILogger <PregeneratedSongProvider> >(),
                           sp.GetService <IHostApplicationLifetime>().ApplicationStopping));
            });
            services.AddSingleton <IRandomSongProvider>(sp => new RandomSongProviderCombinator(
                                                            new WeightedRandom <IRandomSongProvider>(
                                                                new Dictionary <IRandomSongProvider, int> {
                [sp.GetRequiredService <PregeneratedSongProvider>()] = 3,
                [new TopSongWeightedProvider(sp.GetRequiredService <SongVoteCache>())] = 1,
            }
                                                                ), logger: sp.GetService <ILogger <RandomSongProviderCombinator> >()));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        }
 public CachedSongDatabase(
     [NotNull] ILyricsGenerator lyricsGenerator,
     [NotNull] IMemoryCache cache,
     [NotNull] ApplicationDbContext dbContext)
 {
     this.lyricsGenerator = lyricsGenerator ?? throw new ArgumentNullException(nameof(lyricsGenerator));
     this.cache           = cache ?? throw new ArgumentNullException(nameof(cache));
     this.dbContext       = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
 }
Exemple #3
0
 static async void CheckGeneratorSanity(ILyricsGenerator lyricsGenerator)
 => await lyricsGenerator.GenerateLyrics(song : 1362233347, CancellationToken.None)
 .ConfigureAwait(false);