Beispiel #1
0
        /// <summary>
        /// Manage DbConnection - set and check connection.
        /// create new Db if instance not exist
        /// </summary>
        public bool InitializeConnection()
        {
            bool result = false;

            using AppDbContext dbContext = new AppDbContext();
            var dbConnection = dbContext.Database.Connection;

            // check db existance and create if it absent
            if (!dbContext.Database.Exists())
            {
                Log.LogWarning(new DebugMessage($"DB '{dbConnection.Database}' is not found! Create new..."));
                try
                {
                    dbContext.Database.Create();
                    Log.LogSucces(new DebugMessage($"Db '{dbConnection.Database}' created!"));
                }
                catch (Exception e)
                {
                    Log.LogError(new DebugMessage($"Can't create new instance of Db '{dbConnection.Database}'!\r\n" +
                                                  $"ConnectionString: {dbConnection.ConnectionString}\r\nDataSource: {dbConnection.DataSource}", "InitializeConnection()", e));
                    return(false);
                }
            }

            //check connection
            Log.LogSystem(new DebugMessage($"Check Db connection..."));
            try
            {
                dbContext.Database.Connection.Open();
                if (dbContext.Database.Connection.State == ConnectionState.Open)
                {
                    dbContext.Database.Connection.Close();
                    Log.LogSucces(new DebugMessage($"Db '{dbConnection.Database}' connection established!"));
                    result = true;
                }
            }
            catch (Exception e)
            {
                Log.LogError(new DebugMessage($"Can't connect to Db: '{dbConnection.Database}'!\r\n" +
                                              $"ConnectionString: {dbConnection.ConnectionString}\r\nDataSource: {dbConnection.DataSource}",
                                              "InitializeConnection()", e));
            }

            return(result);
        }
Beispiel #2
0
 /// <summary>
 /// 错误回调
 /// </summary>
 /// <param name="state"></param>
 /// <param name="socketCode"></param>
 /// <param name="socketMessage"></param>
 private void CallError(Exception e)
 {
     if (e is SocketException)
     {
         logger.LogException(e);
         logger.LogError("SocketError", ((SocketException)e).SocketErrorCode.ToString());
     }
     if (onError != null)
     {
         onError(currentState, e);
     }
 }
Beispiel #3
0
        /// <summary>
        /// Mark user as compleatly registered in system
        /// </summary>
        public async override Task <bool> CompleateRegistration(IUser user)
        {
            try
            {
                using AppDbContext dbContext = new AppDbContext();
                var dbUser = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == user.UserId);

                dbUser.IsRegistered = true;

                int x = await dbContext.SaveChangesAsync();

                UpdateCache(dbUser);

                // add user log record
                string logMsg = $"User compleate registration - @{user} [UserId = {user.UserId}]";
                await CreateLogMessage(user.UserId, logMsg);

                TBotCore.BotManager.Core.LogController.LogMessage(new DebugMessage(logMsg));
                return(true);
            }
            catch (Exception e)
            {
                Log?.LogError(new TBotCore.Debug.DebugMessage($"Can't compleate registration! [UserId: {user.UserId}, UserName: {user.UserName}]",
                                                              "CompleateRegistration()", e));
                return(false);
            }
        }
Beispiel #4
0
        /// <summary>
        /// 断开连接等待断开成功的回调
        /// </summary>
        public void Disconnect()
        {
            //判断是否已连接
            if (!isConneted)
            {
                logger.LogError("NetPost", "TCP已经断开!");
                return;
            }

            currentState = SocketState.BEGIN_DISCONNECT;
            //设置主动关闭位
            isCloseSelf = true;

            try {
                //开始断开连接
                lock (locker) {
                    socket.BeginDisconnect(false, disconnectCallback, null);
                }
            } catch (Exception exp) {
                //引发报错事件
                CallError(exp);
            }
        }