发现是发布后的程序找不到App.config文件
添加GetConfiguration(), 指定文件路径,可以解决问题:
1 private void SetConfig() 2 { 3 try 4 { 5 Configuration config = GetConfiguration();//ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 6 7 if (config.AppSettings.Settings["dbconnect_read"] != null && config.AppSettings.Settings["dbconnect_write"] != null) 8 { 9 _dbConnectStrRead = config.AppSettings.Settings["dbconnect_read"].Value; 10 _dbConnectStrWrite = config.AppSettings.Settings["dbconnect_write"].Value; 11 _dbContext = new DBContext(_dbConnectStrRead, _dbConnectStrWrite); 12 } 13 } 14 catch (Exception ex) 15 { 16 _logger.Debug(ex.Message); 17 } 18 } 19 20 private static Configuration GetConfiguration() 21 { 22 string configFileName = string.Empty; 23 24 try 25 { 26 ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap(); 27 var path = "./App.config"; 28 configFileMap.ExeConfigFilename = path; 29 Configuration configObj = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); 30 31 return configObj; 32 } 33 catch (Exception ex) 34 { 35 throw ex; 36 } 37 }