예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.

            services.AddMemoryCache();
            services.AddRazorPages().AddRazorRuntimeCompilation();
            services.AddMvc(opt =>
            {
                opt.EnableEndpointRouting = false;
            })
            .AddMvcOptions(options =>
            {
#if DEBUG
                var cacheProfile = new CacheProfile {
                    Location = ResponseCacheLocation.None, NoStore = true
                };
                var cacheProfileNoCache = new CacheProfile {
                    Location = ResponseCacheLocation.None, NoStore = true
                };
#else
                var cacheProfile = new CacheProfile {
                    Duration = 60
                };
                var cacheProfileNoCache = new CacheProfile {
                    Location = ResponseCacheLocation.None, NoStore = true
                };
#endif
                options.CacheProfiles.Add("Default", cacheProfile);
                options.CacheProfiles.Add("Feed", cacheProfile);
                options.CacheProfiles.Add("Embed", cacheProfile);
                options.CacheProfiles.Add("Page", cacheProfile);
                options.CacheProfiles.Add("Archive", cacheProfile);
                options.CacheProfiles.Add("NoCache", cacheProfileNoCache);

                options.Filters.Add(new TypeFilterAttribute(typeof(XFrameOptionsAttribute)));

                options.Filters.Add(new TypeFilterAttribute(typeof(RequirePermanentHttpsAttribute)));

                options.Filters.Add(new ExceptionReportingFilter());
            });

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            //TODO: Change to ServiceLifetime.Scoped once repository is no longer using static methods
            services.AddSingleton(new Func <IServiceProvider, Gov.News.Api.IClient>((serviceProvider) =>
            {
                var client     = new Gov.News.Api.Client();
                client.BaseUri = new Uri(Configuration["NewsApi"]);
                return(client);
            }));



            /*
             * services.AddSingleton(new Func<IServiceProvider, Gcpe.Hub.Services.Legacy.INewslettersClient>((serviceProvider) =>
             * {
             *  var client = new Gcpe.Hub.Services.Legacy.NewslettersClient();
             *  client.BaseUri = new Uri(Configuration.GetConnectionString("HubNewslettersClient"));
             *  return client;
             * }));
             *
             *
             * services.Configure<Data.RepositoryOptions>(Configuration.GetSection("Options:Gov.News.Data:Repository"));
             */


            services.AddSingleton <Repository, Repository>();
            services.AddSingleton <IHostedService, Hubs.LiveHub>();

            // Add the Configuration object so that controllers may use it through dependency injection
            services.AddSingleton <IConfiguration>(Configuration);

            services.Replace(ServiceDescriptor.Singleton(typeof(ILogger <>), typeof(TimestampLogger <>)));

            services
            .AddHealthChecks()
            .AddUrlGroup(new Uri(Configuration["NewsApi"] + "hc"));

            IAsyncPolicy <HttpResponseMessage> cachePolicy =
                Policy.CacheAsync <HttpResponseMessage>(
                    cacheProvider: new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions())),
                    ttl: new TimeSpan(0, 1, 0)
                    );

            services.AddHttpClient("uri-group") // default healthcheck registration name for uri ( you can change it on AddUrlGroup )
            .AddPolicyHandler(cachePolicy);
            services.AddRazorPages().AddRazorRuntimeCompilation();
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.

            services.AddMemoryCache();

            services.AddMvc().AddMvcOptions(options =>
            {
#if DEBUG
                var cacheProfile = new CacheProfile {
                    Location = ResponseCacheLocation.None, NoStore = true
                };
#else
                var cacheProfile = new CacheProfile {
                    Duration = 60
                };
#endif

                options.CacheProfiles.Add("Default", cacheProfile);
                options.CacheProfiles.Add("Feed", cacheProfile);
                options.CacheProfiles.Add("Embed", cacheProfile);
                options.CacheProfiles.Add("Page", cacheProfile);
                options.CacheProfiles.Add("Archive", cacheProfile);

                options.Filters.Add(new TypeFilterAttribute(typeof(XFrameOptionsAttribute)));

                options.Filters.Add(new TypeFilterAttribute(typeof(RequirePermanentHttpsAttribute)));

                options.Filters.Add(new ExceptionReportingFilter());
            });

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            //TODO: Change to ServiceLifetime.Scoped once repository is no longer using static methods
            services.AddSingleton(new Func <IServiceProvider, Gov.News.Api.IClient>((serviceProvider) =>
            {
                var client     = new Gov.News.Api.Client();
                client.BaseUri = new Uri(Configuration["NewsApi"]);
                return(client);
            }));



            /*
             * services.AddSingleton(new Func<IServiceProvider, Gcpe.Hub.Services.Legacy.INewslettersClient>((serviceProvider) =>
             * {
             *  var client = new Gcpe.Hub.Services.Legacy.NewslettersClient();
             *  client.BaseUri = new Uri(Configuration.GetConnectionString("HubNewslettersClient"));
             *  return client;
             * }));
             *
             *
             * services.Configure<Data.RepositoryOptions>(Configuration.GetSection("Options:Gov.News.Data:Repository"));
             */


            services.AddSingleton <Repository, Repository>();
            services.AddSingleton <IHostedService, Hubs.LiveHub>();

            // Add the Configuration object so that controllers may use it through dependency injection
            services.AddSingleton <IConfiguration>(Configuration);

            services.Replace(ServiceDescriptor.Singleton(typeof(ILogger <>), typeof(TimestampLogger <>)));

            // add a health check for the news api service.
            services.AddHealthChecks(checks =>
            {
                checks.AddUrlCheck(Configuration["NewsApi"] + "/hc", new TimeSpan(0, 1, 0));   // check the news client connection every minute.
            });
        }