ASP.NET shedule

private const string ReloadBartCodes_CacheKey = "ReloadBartCodes_CacheKey";
private const string HIT_PAGE_URL = "http://hit_page_url/";
 
        private bool RegisterCacheEntry(string key, string value, int interval)
        {
            if (null != HttpContext.Current.Cache[key]) return false;
 
            HttpContext.Current.Cache.Add(key, value, null,
                                            DateTime.MaxValue, TimeSpan.FromMinutes(interval),
                                            CacheItemPriority.Normal,
                                            new CacheItemRemovedCallback(CacheItemRemovedCallback));
            return true;
 
        }
 
        public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
        {
            if (key == ReloadBartCodes_CacheKey)
            {
                HitPage(Convert.ToString(value));
            }
        }
 
        private void HitPage(string HitPageUrl)
        {
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(HitPageUrl);
                myRequest.Method = "GET";
                myRequest.Timeout = 1000 * 60 * 5;
                WebResponse myResponse = myRequest.GetResponse();
                StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                string result = sr.ReadToEnd();
                sr.Close();
                myResponse.Close();
                log_Global.Info("Hit page OK " + HitPageUrl, ex);
            }
            catch (System.Net.WebException ex)
            {
                log_Error.Fatal("Fatal Error when hitting the page " + HitPageUrl, ex);
            }
 
 
        }
 
        protected void Application_Start(object sender, EventArgs e)
        {
            log_Global.Info("Application started.");
            RegisterCacheEntry(ReloadBartCodes_CacheKey,
                   HIT_PAGE_URL,
                   10);
        }
 
        protected void Session_Start(object sender, EventArgs e)
        {
 
        }
 
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.Url.ToString() == HIT_PAGE_URL)
            {
                RegisterCacheEntry(ReloadBartCodes_CacheKey,
                                   HIT_PAGE_URL,
                                   10);
            }
        }
programming/csharp/aspnetshedule.txt · Last modified: 2018-06-21 19:48 (external edit)
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0