Click here to Skip to main content
15,889,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everybody

i have problems with some code :
I try to cache some value from a config file in the global.asax, with a timer.
The global goal of the project is to check some data and push messages for all webusers (via SignalR) every 20s.

i add dependency on a file, but when updating this file, the cache is not refreshing. i add a absoluteExpiration which is working well.

in the Application_Start of global.asax, i init an application timer:
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    RouteTable.Routes.MapHubs();
    RouteConfig.RegisterRoutes(RouteTable.Routes);

    var aTimer = new System.Timers.Timer(1000);
    aTimer.Elapsed += aTimer_Elapsed;
    aTimer.Interval = 20000;
    aTimer.Enabled = true;

}


the timer tick every 20s (working) :

void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{

    //occur++;

    double currNPM = getCurrentnParMinute(999);


    //UserActivityHub.TimerisTicked(occur);

}


my main function : return a double found in the config file, or in the cache (which is refreshing every 1 minute (ok) or when the file is updated (not ok))

  public double getCurrentnParMinute(int idstructure)
        {
            try
            {
                string cacheName = "getCurrentnParMinute." + idstructure;
                if (System.Web.HttpRuntime.Cache[cacheName] == null)
                {
                    string pathConfig = "c:\myconfig.xml";
                    XmlDocument xmlconfig = new XmlDocument();
                    xmlconfig.Load(pathConfig);

                    double toreturn = 123; /* not real code here, in real i extract this value from pathConfig.file (working) */


                    //CacheDependency dep = 
                    //System.Web.HttpContext.Current.Cache.Insert(cacheName, toreturn, dep); /* not working in global.asax */
                    System.Web.HttpRuntime.Cache.Insert(cacheName, toreturn, 
new CacheDependency(pathConfig), /* that's NOT ok*/
DateTime.Now.AddMinutes(1), /* that's ok*/
System.Web.Caching.Cache.NoSlidingExpiration);
                    return toreturn;
                }
                else
                    return (double)System.Web.HttpRuntime.Cache[cacheName];



            }
            catch (Exception ex)
            {
                return 0;
            }
        }



i have used this system often with no problem on differents web site, the difference here is that i need to use System.Web.HttpRuntime.Cache instead of System.Web.HttpContext.Current.Cache, because (if i understand well) System.Web.HttpContext.Current is null in the global.asax... Any idea what i'm doing wrong ?

What I have tried:

tried to update the file to refresh the cache, no success
Posted
Comments
F-ES Sitecore 9-Nov-18 7:22am    
Does your code have access to that file? If you put it in ~/App_Data instead does it work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900