zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    唱歌循序渐进
    523法则
    南北朝鲜
    全名K歌
    《如何进行接口mock测试》
    vue动态添加路由,跳转页面时,页面报错路由重复:vue-router.esm.js?8c4f:16 [vue-router] Duplicate named routes definition: { name: "Login", path: "/login" }
    npm install -g 和npm install --save-dev的关系
    vue项目安装依赖项的时候总是报错
    聊聊 OAuth 2.0 的 Token 续期处理
    Spring Security OAuth 格式化 token 输出
  • 原文地址:https://www.cnblogs.com/houzf/p/5580089.html
Copyright © 2011-2022 走看看