using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SqlSugar { /// /// Partial SqlSugarScope /// public partial class SqlSugarScope : ISqlSugarClient, ITenant { private List _configs; private Action _configAction; protected virtual SqlSugarClient GetContext() { SqlSugarClient result = null; var key = _configs.GetHashCode().ToString(); StackTrace st = new StackTrace(true); var methods = st.GetFrames(); var isAsync = UtilMethods.IsAnyAsyncMethod(methods); if (methods.Length>=0) { foreach (var method in methods.Take(10)) { var refType = method.GetMethod()?.ReflectedType; if (refType != null) { var getInterfaces = refType.Name.StartsWith("<") ? refType?.ReflectedType?.GetInterfaces() : refType?.GetInterfaces(); if (getInterfaces != null && getInterfaces.Any(it => it.Name.IsIn("IJob"))) { key = $"{key}IJob"; break; } } } } if (isAsync) { result = GetAsyncContext(key); } else { result = GetThreadContext(key); } return result; } private SqlSugarClient GetAsyncContext(string key) { SqlSugarClient result = CallContextAsync.GetData(key); if (result == null) { List configList = GetCopyConfigs(); CallContextAsync.SetData(key, new SqlSugarClient(configList)); result = CallContextAsync.GetData(key); if (this._configAction != null) { this._configAction(result); } } return result; } private SqlSugarClient GetThreadContext(string key) { SqlSugarClient result = CallContextThread.GetData(key); if (result == null) { List configList = GetCopyConfigs(); CallContextThread.SetData(key, new SqlSugarClient(configList)); result = CallContextThread.GetData(key); if (this._configAction != null) { this._configAction(result); } } return result; } private List GetCopyConfigs() { return _configs.Select(it =>UtilMethods.CopyConfig(it)).ToList(); } } }