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)
{
double currNPM = getCurrentnParMinute(999);
}
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;
System.Web.HttpRuntime.Cache.Insert(cacheName, toreturn,
new CacheDependency(pathConfig),
DateTime.Now.AddMinutes(1),
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