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.AddMvc()
            // Use property names as is on classes that get serialized, instead of camelcasing.
            .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
            services.AddOptions();
            services.Configure <Config.HtmlConfig>(Configuration.GetSection("Html"));
            services.Configure <Config.MalApiConfig>(Configuration.GetSection("MalApi"));
            services.Configure <Config.RecommendationsConfig>(Configuration.GetSection("Recommendations"));
            services.Configure <Config.ConnectionStringsConfig>(Configuration.GetSection("ConnectionStrings"));

            services.AddTransient <IAnimeRecsClientFactory, ConfigBasedRecClientFactory>();
            services.AddTransient <IAnimeRecsDbConnectionFactory, ConfigBasedAnimeRecsDbConnectionFactory>();

            IMyAnimeListApi api;

            Config.MalApiConfig apiConfig = Configuration.GetSection("MalApi").Get <Config.MalApiConfig>();
            if (apiConfig.Type == Config.MalApiType.Normal)
            {
                api = new MyAnimeListApi()
                {
                    UserAgent   = apiConfig.UserAgentString,
                    TimeoutInMs = apiConfig.TimeoutMilliseconds,
                };
            }
            else if (apiConfig.Type == Config.MalApiType.DB)
            {
                api = new PgMyAnimeListApi(Configuration.GetConnectionString("AnimeRecs"));
            }
            else
            {
                throw new Exception($"Don't know how to construct MAL API type {apiConfig.Type}.");
            }
            CachingMyAnimeListApi          cachingApi = new CachingMyAnimeListApi(api, TimeSpan.FromSeconds(apiConfig.AnimeListCacheExpirationSeconds), ownApi: true);
            SingletonMyAnimeListApiFactory factory    = new SingletonMyAnimeListApiFactory(cachingApi);

            services.AddSingleton <IMyAnimeListApiFactory>(factory);
        }
        // Called once
        protected override void ConfigureApplicationContainer(TinyIoCContainer container)
        {
            List<IDisposable> disposablesInitialized = new List<IDisposable>();
            try
            {
                // This seems to be called before ApplicationStartup, so read config if it hasn't been read yet
                LoadConfigIfNotLoaded();

                container.Register<IConfig>(AppGlobals.Config);

                IAnimeRecsClientFactory recServiceClientFactory = new RecClientFactory(AppGlobals.Config.RecServicePort, AppGlobals.Config.SpecialRecSourcePorts);
                container.Register<IAnimeRecsClientFactory>(recServiceClientFactory);

                IAnimeRecsDbConnectionFactory dbConnectionFactory = new AnimeRecsDbConnectionFactory(AppGlobals.Config.PostgresConnectionString);
                container.Register<IAnimeRecsDbConnectionFactory>(dbConnectionFactory);

                IMyAnimeListApi api;
                if (AppGlobals.Config.UseLocalDbMalApi)
                {
                    api = new PgMyAnimeListApi(AppGlobals.Config.PostgresConnectionString);
                }
                else
                {
                    api = new MyAnimeListApi()
                    {
                        UserAgent = AppGlobals.Config.MalApiUserAgentString,
                        TimeoutInMs = AppGlobals.Config.MalTimeoutInMs
                    };
                }
                disposablesInitialized.Add(api);

                CachingMyAnimeListApi cachingApi = new CachingMyAnimeListApi(api, AppGlobals.Config.AnimeListCacheExpiration, ownApi: true);
                disposablesInitialized.Add(cachingApi);

                SingletonMyAnimeListApiFactory factory = new SingletonMyAnimeListApiFactory(cachingApi);

                // TinyIoC will dispose of the factory when the Nancy host stops
                container.Register<IMyAnimeListApiFactory>(factory);
            }
            catch
            {
                foreach (IDisposable disposable in disposablesInitialized)
                {
                    disposable.Dispose();
                }
                throw;
            }
        }