webservice緩存服務器數據到本地,提供服務接口讀取緩存的文件


遇到的問題:1、緩存到本地的文件寫入一次,不能讀取,不能二次寫入。程序報異常:文件被占用

              解決辦法:

                           FileStream fs = new FileStream(appPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                           StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);

設置文件FileShare的模式和FileAccess模式,可以解決這個問題

原始代碼為: FileStream fs = new FileStream(appPath, FileMode.Open);

                  StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);

 

webservice 定時緩存文件 需要在網站中加入Gloal.asax文件,在  void Application_Start(object sender, EventArgs e)  (應用程序啟動的時候就會執行)

中添加計時器,定時執行緩存方法

System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Interval = 600000; // 30000 毫秒 = 30秒
timer1.Elapsed += new System.Timers.ElapsedEventHandler(notma.NotamMemory);//執行的方法
timer1.AutoReset = true;//是否重復執行
timer1.Enabled = true;

 

 

 

web 讀取appconfig的方法   string appPath = System.Web.Configuration.WebConfigurationManager.AppSettings["appPath"];

cs讀取和修改appconfig的方法

//獲取Configuration對象
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//根據Key讀取<add>元素的Value
string name = config.AppSettings.Settings["timer"].Value;
//寫入<add>元素的Value
config.AppSettings.Settings["timer"].Value = this.txt_time.Text.Trim();
config.Save(ConfigurationSaveMode.Modified);
//刷新,否則程序讀取的還是之前的值(可能已裝入內存)
System.Configuration.ConfigurationManager.RefreshSection("appSettings");


timer1.Start();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM