private static DataAccessConfig LoadConfig(string xmlPath) { try { var xDoc = XElement.Load(xmlPath); DataAccessConfig config = new DataAccessConfig { DatabaseConfig = new DatabaseConfig { ProviderConfigPath = xDoc.Element("databaseConfig").Element("providers").Value, SqlMapDirectory = xDoc.Element("databaseConfig").Element("sqlMapDirectory").Value, Connections = xDoc.Element("databaseConfig").Element("connections").Elements() .Select(e => new DatabaseConnectionConfig { Name = e.Attribute("name").Value, ConnectionString = e.Attribute("connectionString").Value, DatabaseType = (DatabaseType)Enum.Parse(typeof(DatabaseType), e.Attribute("dbType").Value, true), ProvideName = e.Attribute("provide").Value }).ToArray() }, MongoDBConfig = new MongoDBConfig { Connections = xDoc.Element("mongoDBConfig").Element("connections").Elements() .Select(e => new MongoDBConnectionConfig { Name = e.Attribute("name").Value, Server = e.Attribute("server").Value, Port = int.Parse(e.Attribute("port").Value), Database = e.Attribute("database") != null ? e.Attribute("database").Value : null, Collection = e.Attribute("collection") != null ? e.Attribute("collection").Value : null }).ToArray() } }; return config; } catch { throw new Exception(string.Format("读取数据访问配置出错({0})", xmlPath)); } }
static DataAccessConfig() { string filePath = ApplicationPath.BaseConfigFile; if (File.Exists(filePath)) { instance = LoadConfig(filePath); WatchConfig(filePath); } else { throw new FileNotFoundException("没有找到配置文件", filePath); } }
private static void OnConfigFileChanged(object sender, FileSystemEventArgs e) { lock (instance) { instance = LoadConfig(e.FullPath); } }