[.Net] 編譯調試時正常,發布後無法正常運行
阿新 • • 發佈:2017-12-27
app lena spa 問題: empty mapped name .net str
發現是發布後的程序找不到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 = newDBContext(_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 = newExeConfigurationFileMap(); 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 }
[.Net] 編譯調試時正常,發布後無法正常運行