using System;
using System.Configuration;
using CYQ.Data.SQL;
using CYQ.Data.Tool;
using System.Web;
using System.Collections.Generic;
namespace CYQ.Data
{
///
/// 常用配置文件项[Web(App).Config]的[appSettings|connectionStrings]项和属性名一致。
/// 除了可以从配置文件配置,也可以直接赋值。
///
public static partial class AppConfig
{
#region 基方法
private static MDictionary appConfigs = new MDictionary(StringComparer.OrdinalIgnoreCase);
private static MDictionary connConfigs = new MDictionary(StringComparer.OrdinalIgnoreCase);
///
/// 设置Web.config或App.config的值。
///
public static void SetApp(string key, string value)
{
try
{
if (appConfigs.ContainsKey(key))
{
appConfigs[key] = value;
}
else
{
appConfigs.Add(key, value);
}
}
catch (Exception err)
{
CYQ.Data.Log.WriteLogToTxt(err);
}
}
///
/// 获取Web.config或App.config的值。
///
public static string GetApp(string key)
{
return GetApp(key, string.Empty);
}
///
/// 获取Web.config或App.config的值(允许值不存在或为空时输出默认值)。
///
public static string GetApp(string key, string defaultValue)
{
if (appConfigs.ContainsKey(key))
{
return appConfigs[key];
}
else
{
string value = ConfigurationManager.AppSettings[key];
value = string.IsNullOrEmpty(value) ? defaultValue : value;
try
{
appConfigs.Add(key, value);
}
catch
{
}
return value;
}
}
///
/// 获取Web.config或App.config的数字值(允许值不存在或为空时输出默认值)。
///
public static int GetAppInt(string key, int defaultValue)
{
int result = 0;
string value = GetApp(key);
if (!int.TryParse(value, out result))
{
return defaultValue;
}
return result;
}
///
/// 获取Web.config或App.config的数字值(允许值不存在或为空时输出默认值)。
///
public static bool GetAppBool(string key, bool defaultValue)
{
bool result;
bool.TryParse(GetApp(key, defaultValue.ToString()), out result);
return result;
}
///
/// 获取Web.config或App.config的connectionStrings节点的值。
///
public static string GetConn(string name, out string providerName)
{
providerName = string.Empty;
if (string.IsNullOrEmpty(name))
{
name = "Conn";
}
if (name.Trim().Contains(" "))
{
return name;
}
if (connConfigs.ContainsKey(name))
{
return connConfigs[name];
}
ConnectionStringSettings conn = ConfigurationManager.ConnectionStrings[name];
if (conn != null)
{
providerName = conn.ProviderName;
if (name == conn.ConnectionString)//避免误写自己造成死循环。
{
return name;
}
name = conn.ConnectionString;
if (!string.IsNullOrEmpty(name) && name.Length < 32 && name.Split(' ').Length == 1)
{
return GetConn(name);
}
if (!connConfigs.ContainsKey(name) && string.IsNullOrEmpty(providerName)) // 如果有providerName,则不存档
{
connConfigs.Add(name, conn.ConnectionString);
}
return conn.ConnectionString;
}
if (name.Length > 32 && name.Split('=').Length > 3 && name.Contains(";")) //链接字符串很长,没空格的情况
{
return name;
}
return "";
}
///
/// 获取Web.config或App.config的connectionStrings节点的值。
///
public static string GetConn(string name)
{
string p;
return GetConn(name, out p);
}
///
/// 添加自定义链接(内存有效,并未写入config文件)
///
/// 名称
/// 链接字符串
public static void SetConn(string name, string connectionString)
{
if (!connConfigs.ContainsKey(name))
{
connConfigs.Add(name, connectionString);
}
else
{
connConfigs[name] = connectionString;
}
}
#endregion
///
/// 是否使用表字段枚举转Int方式(默认为false)。
/// 设置为true时,可以加快一点性能,但生成的表字段枚举必须和数据库一致。
///
public static bool IsEnumToInt
{
get
{
return GetAppBool("IsEnumToInt", false);
}
set
{
SetApp("IsEnumToInt", value.ToString());
}
}
///
/// 是否ToJson输出时自动转义特殊符号("\ \r \t等)
/// 可配置项为(Default、Yes、No)
///
public static string JsonEscape
{
get
{
return GetApp("JsonEscape", "Default");
}
set
{
SetApp("JsonEscape", value);
}
}
///
/// Aop 插件配置项 示例配置:[ 完整类名,程序集(dll)名称]<add key="Aop" value="Web.Aop.AopAction,Aop"/>
///
public static string Aop
{
get
{
return GetApp("Aop");
}
set
{
SetApp("Aop", value);
}
}
///
/// Tool.ThreadBreak 使用时,外置的文件配置相对路径(默认在环境变量Temp对应文件中)
///
public static string ThreadBreakPath
{
get
{
return GetApp("ThreadBreakPath", Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.User));
}
set
{
SetApp("ThreadBreakPath", value);
}
}
///
/// 生成的实体类的后缀。
///
public static string EntitySuffix
{
get
{
return GetApp("EntitySuffix", "Bean");
}
set
{
SetApp("EntitySuffix", value);
}
}
///
/// 获取当前Dll的版本号
///
public static string Version
{
get
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
///
/// 框架的运行路径
/// Win项目:是dll和exe所在的目录,
/// Asp.net项目:是指根目录
/// Asp.net core 项目:是指运行的路径(dll所在的路径)
///
public static string RunPath
{
get
{
if (AppConfig.IsWeb)
{
return AppDomain.CurrentDomain.BaseDirectory;
}
return AppConst.RunFolderPath;
}
}
}
public static partial class AppConfig
{
private static string _WebRootPath;
//内部变量
///
/// Web根目录(ASP.NET Core 项目时,由于机制不同,指向的路径需要调整,所以该值可以修改)
///
public static string WebRootPath
{
get
{
if (string.IsNullOrEmpty(_WebRootPath))
{
_WebRootPath = AppDomain.CurrentDomain.BaseDirectory;
}
if (!_WebRootPath.EndsWith("\\") && !_WebRootPath.EndsWith("/"))
{
_WebRootPath = _WebRootPath + "\\";
}
return _WebRootPath;
}
set
{
_WebRootPath = value;
}
}
internal static bool IsWeb
{
get
{
return HttpContext.Current != null;
}
}
internal static Uri WebUri
{
get
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Request.Url;
}
return null;
}
}
//读配置文件时会修改此值。
private static bool _IsAspNetCore = false;
///
/// 当前是否ASP.NET Core环境。
///
public static bool IsAspNetCore
{
get
{
return _IsAspNetCore;
}
set
{
_IsAspNetCore = value;
}
}
}
public static partial class AppConfig
{
#region Xml相关配置
///
/// XHtml 相关的配置
///
public static class XHtml
{
///
/// Xml.XHtmlHelper 中使用的 "<![CDATA[" 项
///
internal static string CDataLeft
{
get
{
return GetApp("CDataLeft", "
/// Xml.XHtmlHelper 中使用的 "]]>" 项
///
internal static string CDataRight
{
get
{
return GetApp("CDataRight", "::MMS]]>");
}
set
{
SetApp("CDataRight", value);
}
}
///
/// Xml.XHtmlHelper 中操作Html需要配置的DTD解析文档相对路径
///
public static string DtdUri
{
get
{
string dtdUri = GetApp("DtdUri", "/Setting/DTD/xhtml1-transitional.dtd");
if (dtdUri != null && dtdUri.IndexOf("http://") == -1)//相对路径
{
dtdUri = AppConfig.WebRootPath + dtdUri.TrimStart('/').Replace("/", "\\");
}
return dtdUri;
}
set
{
SetApp("DtdUri", value);
}
}
private static string _Domain;
///
/// Xml.MutilLanguage 语言切换设置时Cookie所需要的网站主域名[不带www]
///
public static string Domain
{
get
{
if (string.IsNullOrEmpty(_Domain))
{
Uri uri = AppConfig.WebUri;
string domainList = GetApp("Domain", "");
if (!string.IsNullOrEmpty(domainList))
{
string[] domains = domainList.Split(',');
if (domains != null && domains.Length > 0)
{
if (domains.Length == 1)
{
_Domain = domains[0];
}
else if (uri != null)
{
foreach (string domain in domains)
{
if (uri.Authority.Contains(domain))
{
_Domain = domain;
}
}
}
}
}
else if (uri != null && uri.Host != "localhost")
{
_Domain = uri.Host.Replace("www.", string.Empty);
}
}
return _Domain;
}
set
{
SetApp("Domain", value);
}
}
//private static int _UserFileLoadXml = -1;
/////
///// Xml.XHtmlHelper 操作Html时,配置此项使用File加载Xml[便可在IIS7以上非信任主机机制下使用]
/////
//public static bool UseFileLoadXml
//{
// get
// {
// if (_UserFileLoadXml == -1)
// {
// _UserFileLoadXml = GetApp("UseFileLoadXml") == "true" ? 1 : 0;
// }
// return _UserFileLoadXml == 1;
// }
// set
// {
// _UserFileLoadXml = value ? 1 : 0;
// }
//}
///
/// Xml.MutilLanguage 类的默认语言Key,默认值:Chinese
///
public static string SysLangKey
{
get
{
return GetApp("SysLangKey", "Chinese");
}
set
{
SetApp("SysLangKey", value);
}
}
}
#endregion
#region 数据库相关
///
/// DataBase 相关的配置
///
public static class DB
{
static string _DefaultConn = string.Empty;
///
/// 默认数据库链接(可赋完整链接语句或Web.config配置项名称)
/// 如果不在配置文件(Web.Config)上配置Conn链接,可对此属性赋值进行配置。
///
public static string DefaultConn
{
get
{
if (string.IsNullOrEmpty(_DefaultConn))
{
_DefaultConn = "Conn";
if (ConfigurationManager.ConnectionStrings != null && ConfigurationManager.ConnectionStrings[_DefaultConn] == null)
{
foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
{
if (item.Name.ToLower().EndsWith("conn"))
{
_DefaultConn = item.Name;
break;
}
}
}
}
return _DefaultConn;
}
set
{
if (value == null)
{
value = string.Empty;
}
_DefaultConn = value;
}
}
/*
static string _DefaultConnBak = string.Empty;
///
/// 默认备用数据库链接(当主数据挂掉时,会自动切换到备用数据库链接)
/// 如果不在配置文件(Web.Config)上配置Conn_Bak链接,可对此属性赋值进行配置。
///
public static string DefaultConnBak
{
get
{
if (_DefaultConnBak == string.Empty && _DefaultConn.Length < 32 && _DefaultConn.Split(' ').Length == 1)
{
return _DefaultConn + "_Bak";
}
return _DefaultConnBak;
}
set
{
_DefaultConnBak = value;
}
}
*/
private static string _DefaultDataBase;
///
/// 默认数据库名称(只读)
///
public static string DefaultDataBase
{
get
{
if (string.IsNullOrEmpty(_DefaultDataBase))
{
SetDefault();
}
return _DefaultDataBase;
}
}
private static DalType _DefaultDalType = DalType.None;
///
/// 默认数据库类型(只读)
///
public static DalType DefaultDalType
{
get
{
if (_DefaultDalType == DalType.None)
{
SetDefault();
}
return _DefaultDalType;
}
}
private static void SetDefault()
{
DbBase db = DalCreate.CreateDal(DefaultConn);
if (db != null)
{
_DefaultDataBase = db.DataBase;
_DefaultDalType = db.dalType;
db.Dispose();
}
}
///
/// MSSQL是否启用分页存储过程SelectBase,默认false
///
public static bool PagerBySelectBase
{
get
{
return GetAppBool("PagerBySelectBase", false);
}
set
{
SetApp("PagerBySelectBase", value.ToString());
}
}
///
/// 配置此项时,会对:插入/更新/删除的操作进行Lock[请适当使用]
///
//public static bool LockOnDbExe
//{
// get
// {
// bool _LockOnDbExe;
// bool.TryParse(GetApp("LockOnDbExe"), out _LockOnDbExe);
// return _LockOnDbExe;
// }
// set
// {
// SetApp("LockOnDbExe", value.ToString());
// }
//}
///
/// MAction所有操作中的where条件,默认有超强的过滤单词,来过滤Sql注入关键字,如果语句包含指定的过滤词,则会返回错误信息,并记录日志。
/// 如果需要自定义关键字,可配置此项,如:“delete;from,truncate,其它单词”,分号表词组,需要同时包含两个词; 多个过滤词组以","逗号分隔
///
public static string FilterSqlInjection
{
get
{
return GetApp("FilterSqlInjection", SqlInjection.filterSqlInjection);
}
set
{
SetApp("FilterSqlInjection", value);
if (!string.IsNullOrEmpty(value))
{
List list = new List();
list.AddRange(value.TrimEnd(',').Split(','));
SqlInjection.FilterKeyList = list;
}
}
}
/*
///
/// MAction所有操作中的where条件,会被替换为空的字符,默认为“--“。
/// 如果需要自定义,可配置此项,如:“--,;“ 多个替换符号以逗号分隔
///
public static string ReplaceSqlInjection
{
get
{
return GetApp("ReplaceSqlInjection", SqlInjection.replaceSqlInjection);
}
set
{
SetApp("ReplaceSqlInjection", value);
}
}
*/
///
/// MAction 操作 Oracle 时自增加int类型ID所需要配置的序列ID,Guid为ID则不用。
/// 如果需要为每个表都配置一个序列号,可以使用:SEQ_{0} 其中{0}会自动配对成表名,如果没有{0},则为整个数据库共用一个序列。
/// 默认参数值:SEQ_{0}
///
public static string AutoID
{
get
{
return GetApp("AutoID", "SEQ_{0}");
}
set
{
SetApp("AutoID", value);
}
}
///
/// MAction 可将表架构映射到外部指定相对路径[外部存储,可避开数据库读取]
///
public static string SchemaMapPath
{
get
{
string path = GetApp("SchemaMapPath", string.Empty);
if (!string.IsNullOrEmpty(path) && !path.EndsWith("\\"))
{
path = path.TrimEnd('/') + "\\";
}
return path;
}
set
{
SetApp("SchemaMapPath", value);
}
}
///
/// 删除字段名称(若表存在此设置的字段名称时,MActon的删除操作将变更变为更新操作)
/// 默认:IsDeleted
///
public static string DeleteField
{
get
{
return GetApp("DeleteField", "IsDeleted");
}
set
{
SetApp("DeleteField", value);
}
}
///
/// 更新时间字段名称(若表存在指定字段名称时,自动更新时间,多个用逗号分隔)
///
public static string EditTimeFields
{
get
{
return GetApp("EditTimeFields", string.Empty);
}
set
{
SetApp("EditTimeFields", value);
}
}
///
///系统全局要隐藏的字段名称(默认值为:"cyqrownum,rowguid,deletefield")
///
public static string HiddenFields
{
get
{
string result = GetApp("HiddenFields", "cyqrownum,rowguid");
//if (result == string.Empty)
//{
// result = "cyqrownum,rowguid," + DeleteField;
//}
return result;
}
set
{
if (!value.Contains("cyqrownum,rowguid"))
{
value = "cyqrownum,rowguid," + value;
}
SetApp("HiddenFields", value);
}
}
///
/// 全局的数据库命令默认超时设置,默认值120秒(单位:秒)
///
public static int CommandTimeout
{
get
{
return GetAppInt("CommandTimeout", 120);
}
set
{
SetApp("CommandTimeout", value.ToString());
}
}
///
/// 读写分离时用户对主数据库操作持续时间,默认值10秒s(单位:秒s)
///
public static int MasterSlaveTime
{
get
{
return GetAppInt("MasterSlaveTime", 10);
}
set
{
SetApp("MasterSlaveTime", value.ToString());
}
}
}
#endregion
#region 缓存相关配置
///
/// 缓存相关的配置
///
public static class Cache
{
///
/// MemCache分布式缓存的服务器配置,多个用逗号(,)分隔
///
public static string MemCacheServers
{
get
{
return GetApp("MemCacheServers", string.Empty);
}
set
{
SetApp("MemCacheServers", value);
}
}
///
/// MemCache 备份服务器(当主服务器挂了后,请求会转向备用机)
///
public static string MemCacheServersBak
{
get
{
return GetApp("MemCacheServersBak", string.Empty);
}
set
{
SetApp("MemCacheServersBak", value);
}
}
///
/// Redis分布式缓存的服务器配置,多个用逗号(,)分隔
/// 格式:ip:port - password
///
public static string RedisServers
{
get
{
return GetApp("RedisServers", string.Empty);
}
set
{
SetApp("RedisServers", value);
}
}
///
/// Redis 使用的DB数(默认1,使用db0)
///
public static int RedisUseDBCount
{
get
{
return GetAppInt("RedisUseDBCount", 1);
}
set
{
SetApp("RedisUseDBCount", value.ToString());
}
}
///
/// Redis 使用的DB 索引(默认0,若配置,则会忽略RedisUseDBCount)
///
public static int RedisUseDBIndex
{
get
{
return GetAppInt("RedisUseDBIndex", 0);
}
set
{
SetApp("RedisUseDBIndex", value.ToString());
}
}
///
/// Redis 备份服务器(当主服务器挂了后,请求会转向备用机),多个用逗号(,)分隔
/// 格式:ip:port - password
///
public static string RedisServersBak
{
get
{
return GetApp("RedisServersBak", string.Empty);
}
set
{
SetApp("RedisServersBak", value);
}
}
///
/// Cache.CacheManage 默认缓存项的时间[分钟(默认60)]
///
public static int DefaultCacheTime
{
get
{
return GetAppInt("DefaultCacheTime", 60);
}
set
{
SetApp("DefaultCacheTime", value.ToString());
}
}
///
/// 是否智能缓存数据(默认开启)
///
public static bool IsAutoCache
{
get
{
return GetAppBool("IsAutoCache", true);
}
set
{
SetApp("IsAutoCache", value.ToString());
}
}
///
/// AutoCache开启时,可以设置不缓存的Table,多个用逗号分隔
///
public static string NoCacheTables
{
get
{
return GetApp("NoCacheTables", "");
}
set
{
SetApp("NoCacheTables", value);
CYQ.Data.Cache.AutoCache.NoCacheTables = null;
}
}
///
/// AutoCache开启时,可以设置不受更新影响的列名,用Json格式。
/// {talbeName1:'column1,column2',talbeName2:'column1,column2'}
///
public static string IngoreCacheColumns
{
get
{
return GetApp("IngoreCacheColumns", "");
}
set
{
SetApp("IngoreCacheColumns", value);
CYQ.Data.Cache.AutoCache.IngoreCacheColumns = null;
}
}
private static string _AutoCacheConn = null;
///
/// CYQ.Data.Cache 自动缓存 - 数据库链接配置
/// 在多个不同的应用项目里操作同一个数据库时(又不想使用分布式缓存MemCache或Redis),可以开启此项,达到缓存智能清除的效果。
///
public static string AutoCacheConn
{
get
{
if (_AutoCacheConn == null)
{
_AutoCacheConn = AppConfig.GetConn("AutoCacheConn");
}
return _AutoCacheConn;
}
set
{
_AutoCacheConn = value;
}
}
///
/// 当AutoCacheConn开启后,定时扫描数据库的任务时间(毫秒),默认1000
///
public static int AutoCacheTaskTime
{
get
{
return GetAppInt("AutoCacheTaskTime", 1000);
}
set
{
SetApp("AutoCacheTaskTime", value.ToString());
}
}
/*
///
/// Cache.CacheManage 内置线程-缓存的同步时间[(默认5)分钟同步一次]
///
public static int CacheClearWorkTime
{
get
{
return GetAppInt("CacheClearWorkTime", 5);
}
set
{
SetApp("CacheClearWorkTime", value.ToString());
}
}
///
/// Cache.CacheManage 内置线程-调用次数:[N(默认4)分钟内调用次数少于指定值(默认2),缓存即被清除]
///
public static int CacheClearCallCount
{
get
{
return GetAppInt("CacheClearCallCount", 2);
}
set
{
SetApp("CacheClearCallCount", value.ToString());
}
}
///
/// Cache.CacheManage 内置线程-时间设置:[N(默认4)分钟内调用次数少于指定值(默认2),缓存即被清除]
///
public static int CacheClearTime
{
get
{
return GetAppInt("CacheClearTime", 4);
}
set
{
SetApp("CacheClearTime", value.ToString());
}
}
*/
}
#endregion
#region 日志相关配置
///
/// 日志类Log 相关的配置
///
public static class Log
{
private static string _LogConn = null;
///
/// CYQ.Data.Log 类记录数据库异常日志 - 数据库链接配置
///
public static string LogConn
{
get
{
if (_LogConn == null)
{
_LogConn = AppConfig.GetConn("LogConn");
}
return _LogConn;
}
set
{
_LogConn = value;
}
}
private static string _LogPath;
///
/// 文本日志的配置相对路径(默认为:Logs\\")
///
public static string LogPath
{
get
{
if (string.IsNullOrEmpty(_LogPath))
{
_LogPath = AppConfig.GetApp("LogPath", "Logs\\");
if (!_LogPath.EndsWith("\\"))
{
_LogPath = _LogPath.TrimEnd('/') + "\\";
}
}
return _LogPath;
}
set
{
_LogPath = value;
if (!_LogPath.EndsWith("\\"))
{
_LogPath = _LogPath.TrimEnd('/') + "\\";
}
}
}
///
/// 是否写数据库异常日志:开启时:有异常不抛出,转写入数据库;不开启:有异常会抛出
///
public static bool IsWriteLog
{
get
{
return GetAppBool("IsWriteLog", false);
}
set
{
SetApp("IsWriteLog", value.ToString());
}
}
private static string _LogTableName;
///
/// 异常日志表名(默认为SysLogs,可配置)
///
public static string LogTableName
{
get
{
if (string.IsNullOrEmpty(_LogTableName))
{
_LogTableName = AppConfig.GetApp("LogTableName", "SysLogs");
}
return _LogTableName;
}
set
{
_LogTableName = value;
}
}
}
#endregion
#region 调试类相关的配置
///
/// 调试类AppDebug 相关的配置
///
public class Debug
{
#region 配置文件的其它属性
///
///毫秒数(这个是在对所有SQL语句的:将所有长时间(ms)的SQL语句写入日志,对应配置项LogPath的路径)
///
public static int SqlFilter
{
get
{
return GetAppInt("SqlFilter", -1);
}
set
{
SetApp("SqlFilter", value.ToString());
}
}
///
/// 毫秒数(这个是在AppDebug开启后的:可通过此项设置条件过滤出时间(ms)较长的SQL语句)
///
public static int InfoFilter
{
get
{
return GetAppInt("InfoFilter", 0);
}
set
{
SetApp("InfoFilter", value.ToString());
}
}
///
/// 开启信息调试记录:开启后MAction.DebugInfo可输出执行日志。
/// 同时AppDebug若要使用,也需要开启此项。
///
public static bool OpenDebugInfo
{
get
{
return GetAppBool("OpenDebugInfo", false);
}
set
{
SetApp("OpenDebugInfo", value.ToString());
}
}
#endregion
}
#endregion
#region UI 相关的配置
public static class UI
{
///
/// UI取值的默认前缀(ddl,chb,txt),多个用逗号(,)分隔
///
public static string AutoPrefixs
{
get
{
return GetApp("AutoPrefixs", "txt,chb,ddl");
}
set
{
SetApp("AutoPrefixs", value);
}
}
}
#endregion
}
}