public static void UseHostedWhitelist( this IApplicationBuilder app, string url, HttpClient client = null) { // Shared HTTP client for all requests if (client == null) { // Use IMemoryCache from DI if available, e.g. if configured // with services.AddMemoryCache() var cache = (MemoryCache)app.ApplicationServices .GetService(typeof(IMemoryCache)) ?? new MemoryCache(new MemoryCacheOptions()); // Default to 5 minutes caching if not forbidden by NoStore // or specified by Cache-Control headers. var handler = new CachingHttpHandler( new HttpClientHandler(), cache); handler.DefaultCacheDuration = new TimeSpan(0, 5, 0); client = new HttpClient(handler); } // GetStringAsync is called on every IP check, but HttpClient // will honour caching headers app.UseWhitelist(async() => Whitelist.Parse(await client.GetStringAsync(url))); }
public void Init(HttpApplication context) { var handler = new CachingHttpHandler( new HttpClientHandler(), new MemoryCache(new MemoryCacheOptions())); if (TimeSpan.TryParse( ConfigurationManager.AppSettings["whitelist:DefaultCacheDuration"], out var duration)) { handler.DefaultCacheDuration = duration; } _client = new HttpClient(handler); _url = ConfigurationManager.AppSettings["whitelist:Url"]; var eventHandler = new EventHandlerTaskAsyncHelper(beginRequest); context.AddOnBeginRequestAsync( eventHandler.BeginEventHandler, eventHandler.EndEventHandler); }