public ServiceContext(IDbDomain dbDomain, string dbName) { if (dbDomain == null) { throw new ArgumentNullException("dbDomain"); } if (string.IsNullOrEmpty(dbName)) { throw new ArgumentNullException("_dbName"); } this._currentThreadID = Thread.CurrentThread.ManagedThreadId; this._dbDomain = dbDomain; this._dataContext = dbDomain.DataProvider.OpenDataContext(dbName); this.UserSessionService = new SqlUserSessionStore(this._dataContext); try { this._transaction = this.DataContext.BeginTransaction(); try { this.UserSession = UserSession.CreateSystemUserSession(); this.UserSessionService.Put(this.UserSession); } catch { this.DbTransaction.Rollback(); this.DbTransaction.Dispose(); throw; } } catch { this.DataContext.Close(); this.disposed = true; throw; } }
/// <summary> /// 构造一个使用 'system' 用户登录的 ServiceScope /// </summary> /// <param name="ctx"></param> public ServiceContext(IDbDomain dbDomain, IDataContext ctx) { if (dbDomain == null) { throw new ArgumentNullException("dbDomain"); } if (ctx == null) { throw new ArgumentNullException("session"); } this._currentThreadID = Thread.CurrentThread.ManagedThreadId; this._dbDomain = dbDomain; this._dataContext = ctx; this.UserSessionService = new SqlUserSessionStore(this._dataContext); try { this._transaction = this.DataContext.BeginTransaction(); try { this.UserSession = UserSession.CreateSystemUserSession(); this.UserSessionService.Put(this.UserSession); } catch { this.DbTransaction.Rollback(); this.DbTransaction.Dispose(); throw; } } catch { this.DataContext.Close(); this.disposed = true; throw; } }
public void BeforeTest() { Debug.Assert(this.Context == null); Debug.Assert(!string.IsNullOrEmpty(this.SessionToken)); this._dbDomain = SlipstreamEnvironment.DbDomains.GetDbDomain(TestingDatabaseName); this.Context = _dbDomain.OpenSession(this.SessionToken); }
/// <summary> /// 安全的创建 Context,会检查 session 等 /// </summary> /// <param name="sessionToken"></param> public ServiceContext(IDbDomain dbDomain, string dbName, string sessionToken) { if (dbDomain == null) { throw new ArgumentNullException("dbDomain"); } if (string.IsNullOrEmpty(dbName)) { throw new ArgumentNullException("_dbName"); } if (string.IsNullOrEmpty(sessionToken)) { throw new ArgumentNullException("sessionToken"); } this._currentThreadID = Thread.CurrentThread.ManagedThreadId; this._dbDomain = dbDomain; this._dataContext = dbDomain.DataProvider.OpenDataContext(dbName); this.UserSessionService = new SqlUserSessionStore(this._dataContext); var session = this.UserSessionService.GetByToken(sessionToken); if (session == null || !session.IsActive) { //删掉无效的 Session this.UserSessionService.Remove(session.Token); throw new SlipStream.Exceptions.SecurityException("Not logged!"); } try { this._transaction = this.DataContext.BeginTransaction(); try { this.UserSessionService.Pulse(sessionToken); this.UserSession = session; } catch { this.DbTransaction.Rollback(); this.DbTransaction.Dispose(); throw; } } catch { this.DataContext.Close(); this.disposed = true; throw; } }