当前位置: 代码迷 >> ASP.NET >> 修改config后再次读取不能读取修改后的解决思路
  详细解决方案

修改config后再次读取不能读取修改后的解决思路

热度:4109   发布时间:2013-02-25 00:00:00.0
修改config后再次读取不能读取修改后的
页面加载的时候会先调用locked方法
C# code
static object obj = new object();        protected string locked()        {            CMLogger m_logger = CMLogger.GetInstance();            string errcode = "0000";            lock (obj)            {                string FindVPN_Address = System.Configuration.ConfigurationManager.AppSettings["VPN_Address"];                string VPN_Address = "";                string MasterVPN_Address = System.Configuration.ConfigurationManager.AppSettings["Master_VpnAddress"];                #region                if (FindVPN_Address == MasterVPN_Address)                {                    VPN_Address = FindVPN_Address;                }                else if (!SetParam(MasterVPN_Address))                {                    ChangeChannel changechannel = new ChangeChannel();                    changechannel.ChangeVPN_Address(MasterVPN_Address);                    VPN_Address = MasterVPN_Address;                }                else                {                    VPN_Address = FindVPN_Address;                }                if (SetParam(VPN_Address))                {                    int count = 0;                    for (int i = 0; i < 2; i++)                    {                        if (SetParam(VPN_Address))                        {                            count++;                            if (count == 2)                            {                                ChangeChannel channle = new ChangeChannel();                                errcode = channle.ChangeBocChannel(ConfigurationManager.AppSettings["MonitorSiteName"]);                            }                        }                        else                            break;                    }                }                return errcode;                #endregion            }        }


后边会对config中VPN_Address这个节点进行修改
C# code
public void ChangeVPN_Address(string NPN_Address)        {            CMLogger m_logger = CMLogger.GetInstance();            try            {                Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/", ConfigurationManager.AppSettings["MonitorSiteName"]);                AppSettingsSection appseting = (AppSettingsSection)config.GetSection("appSettings");                appseting.Settings["Bank_VPNRuntime"].Value = NPN_Address;                     config.Save();                ConfigurationManager.RefreshSection("appSettings");                config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");                appseting = (AppSettingsSection)config.GetSection("appSettings");                appseting.Settings["VPN_Address"].Value = NPN_Address;                config.Save();                ConfigurationManager.RefreshSection("appSettings");            }            catch (Exception ex)            {                m_logger.WriteDebugLog("保存失败【" + ex + "】");            }        }


当多个进程同时访问config的时候,由于前边加了lock,所以会第二个以后的进程会等待第一个结束后才会继续执行,但是当第一个进程修改过config后,第二个进程执行时拿到的config节点的值还是未修改的,这个是怎么回事,坐等给为大神指点

------解决方案--------------------------------------------------------
config中保存一些小的,不用修改的数据,比如sql连接字符串,定义某程序需要读取的路径等等 .
你需要修改的数据还是放到数据库中吧,需要的时候直接从数据库读取
------解决方案--------------------------------------------------------
貌似xml可以在这里面应用一下,问题出现的原因就是因为数据修改。linq to xml
  相关解决方案