public void Closing(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
        {
            var stats = ((SqlConnection)connection).RetrieveStatistics();
            var bytesReceived = stats["BytesReceived"];

            // TODO: Observe the database response size using the monitoring system of your choice.
        }
            public void Cloning_the_interception_context_preserves_contextual_information_but_not_mutable_state()
            {
                var objectContext = new ObjectContext();
                var dbContext = DbContextMockHelper.CreateDbContext(objectContext);

                var interceptionContext = new DbConnectionInterceptionContext<int>();
                interceptionContext.Exception = new Exception("Cheez Whiz");
                interceptionContext.Result = 23;
                interceptionContext.UserState = "Red Windsor";

                interceptionContext = interceptionContext
                    .WithDbContext(dbContext)
                    .WithObjectContext(objectContext)
                    .AsAsync();

                Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts);
                Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts);
                Assert.True(interceptionContext.IsAsync);

                Assert.Equal(0, interceptionContext.Result);
                Assert.Equal(0, interceptionContext.OriginalResult);
                Assert.Null(interceptionContext.Exception);
                Assert.Null(interceptionContext.OriginalException);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                Assert.Null(interceptionContext.UserState);
            }
        public void Dispose_executes_operation_and_dispatches_to_interceptors()
        {
            var mockConnection = new Mock<DbConnection>();
            var mockInterceptor = new Mock<IDbConnectionInterceptor>();
            var dispatcher = new DbConnectionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(mockInterceptor.Object);

            var interceptionContext = new DbConnectionInterceptionContext();
            dispatcher.Dispose(mockConnection.Object, interceptionContext);

            mockConnection.Protected().Verify("Dispose", Times.Once(), ItExpr.IsAny<bool>());
            mockInterceptor.Verify(m => m.Disposing(mockConnection.Object, It.IsAny<DbConnectionInterceptionContext>()), Times.Once());
            mockInterceptor.Verify(m => m.Disposed(mockConnection.Object, It.IsAny<DbConnectionInterceptionContext>()), Times.Once());
        }
            public void Initially_has_no_state()
            {
                var interceptionContext = new DbConnectionInterceptionContext<int>();

                Assert.Empty(interceptionContext.DbContexts);
                Assert.Null(interceptionContext.Exception);
                Assert.False(interceptionContext.IsAsync);
                Assert.False(interceptionContext.IsExecutionSuppressed);
                Assert.Empty(interceptionContext.ObjectContexts);
                Assert.Null(interceptionContext.OriginalException);
                Assert.Equal(0, interceptionContext.OriginalResult);
                Assert.Equal(0, interceptionContext.Result);
                Assert.Equal((TaskStatus)0, interceptionContext.TaskStatus);
                Assert.Null(interceptionContext.UserState);
            }
 public void Disposed(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
 }
 public void DataSourceGot(DbConnection connection, DbConnectionInterceptionContext<string> interceptionContext)
 {
 }
 public void ConnectionTimeoutGot(DbConnection connection, DbConnectionInterceptionContext<int> interceptionContext)
 {
 }
 public void Closing(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
 }
        public void Open_executes_operation_and_dispatches_to_interceptors()
        {
            var mockConnection = new Mock<DbConnection>();
            var mockInterceptor = new Mock<IDbConnectionInterceptor>();
            var dispatcher = new DbConnectionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(mockInterceptor.Object);

            var interceptionContext = new DbConnectionInterceptionContext();
            dispatcher.Open(mockConnection.Object, interceptionContext);

            mockConnection.Verify(m => m.Open(), Times.Once());
            mockInterceptor.Verify(m => m.Opening(mockConnection.Object, It.IsAny<DbConnectionInterceptionContext>()), Times.Once());
            mockInterceptor.Verify(m => m.Opened(mockConnection.Object, It.IsAny<DbConnectionInterceptionContext>()), Times.Once());
        }
        public void OpenAsync_executes_operation_and_dispatches_to_interceptors()
        {
            var mockConnection = new Mock<DbConnection>();
            mockConnection.Setup(m => m.OpenAsync(It.IsAny<CancellationToken>())).Returns(() => Task.FromResult(true));
            var mockInterceptor = new Mock<IDbConnectionInterceptor>();
            var dispatcher = new DbConnectionDispatcher();
            var internalDispatcher = dispatcher.InternalDispatcher;
            internalDispatcher.Add(mockInterceptor.Object);

            var interceptionContext = new DbConnectionInterceptionContext();
            dispatcher.OpenAsync(mockConnection.Object, interceptionContext, CancellationToken.None).Wait();

            mockConnection.Verify(m => m.OpenAsync(CancellationToken.None), Times.Once());
            mockInterceptor.Verify(m => m.Opening(mockConnection.Object, It.IsAny<DbConnectionInterceptionContext>()), Times.Once());
            mockInterceptor.Verify(m => m.Opened(mockConnection.Object, It.IsAny<DbConnectionInterceptionContext>()), Times.Once());
        }
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void ConnectionTimeoutGot(DbConnection connection, DbConnectionInterceptionContext <int> interceptionContext)
 {
 }
 /// <summary>
 /// 在获取数据库连接字符串动作执行前瞬间触发。
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="interceptionContext"></param>
 public virtual void ConnectionStringGetting(DbConnection connection, DbConnectionInterceptionContext<string> interceptionContext)
 {
 }
 public void ServerVersionGot(DbConnection connection, DbConnectionInterceptionContext<string> interceptionContext)
 {
 }
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void StateGot(DbConnection connection, DbConnectionInterceptionContext <ConnectionState> interceptionContext)
 {
 }
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void ServerVersionGot(DbConnection connection, DbConnectionInterceptionContext <string> interceptionContext)
 {
 }
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="connection">The connection that was disposed.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void Disposed(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
 }
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void DataSourceGot(DbConnection connection, DbConnectionInterceptionContext <string> interceptionContext)
 {
 }
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void DatabaseGetting(DbConnection connection, DbConnectionInterceptionContext <string> interceptionContext)
 {
 }
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void ConnectionStringGot(DbConnection connection, DbConnectionInterceptionContext <string> interceptionContext)
 {
 }
 /// <summary>
 /// 在打开数据库连接动作执行前瞬间触发。将数据库连接字符串更新至 EF 数据库主从读写分离服务中配置的相关值。
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="interceptionContext"></param>
 public override void Opening(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
     this.UpdateConnectionStringIfNeed(connection, this.Config.UsableMasterConnectionString, interceptionContext.DbContexts);
 }
 public void Opened(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
     ((SqlConnection)connection).StatisticsEnabled = true;
 }
 public void Opened_logs_exceptions_async()
 {
     var writer = new StringWriter();
     var interceptionContext = new DbConnectionInterceptionContext().AsAsync();
     interceptionContext.Exception = new Exception("Boo");
     new DatabaseLogFormatter(writer.Write).Opened(new Mock<DbConnection>().Object, interceptionContext);
     Assert.True(
         _resourceVerifier.IsMatch("ConnectionOpenErrorLogAsync", GetSingleLine(writer), new AnyValueParameter(), "Boo", ""));
 }
 public void Opening(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
     if (connection.Database == "master")
     {
         interceptionContext.Exception =
             (SqlException)Activator.CreateInstance(
                 typeof(SqlException), BindingFlags.Instance | BindingFlags.NonPublic, null,
                 new object[] { "No master for you!", null, null, Guid.NewGuid() }, null);
     }
 }
 public void Opened_logs_canceled()
 {
     var writer = new StringWriter();
     var interceptionContext = new DbConnectionInterceptionContext().AsAsync();
     interceptionContext.MutableData.TaskStatus = TaskStatus.Canceled;
     new DatabaseLogFormatter(writer.Write).Opened(new Mock<DbConnection>().Object, interceptionContext);
     Assert.True(_resourceVerifier.IsMatch("ConnectionOpenCanceledLog", GetSingleLine(writer), new AnyValueParameter(), ""));
 }
 /// <summary>
 /// 在获取数据库连接超时时间动作执行前瞬间触发。
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="interceptionContext"></param>
 public virtual void ConnectionTimeoutGetting(DbConnection connection, DbConnectionInterceptionContext<int> interceptionContext)
 {
 }
 /// <summary>
 /// 在获取 数据源服务器名称/IP 名称动作执行前瞬间触发。
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="interceptionContext"></param>
 public virtual void DataSourceGetting(DbConnection connection, DbConnectionInterceptionContext<string> interceptionContext)
 {
 }
 public void Closed(DbConnection connection,
                    DbConnectionInterceptionContext interceptionContext)
 {
     var context = EFProfilerContextProvider.GetLoggedDbConnection(connection, interceptionContext);
     _profiler.ConnectionClosed(connection, context);
 }
 /// <summary>
 /// 在获取数据库名称动作完成后瞬间触发。
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="interceptionContext"></param>
 public virtual void DatabaseGot(DbConnection connection, DbConnectionInterceptionContext<string> interceptionContext)
 {
 }
 public void ConnectionStringGot(DbConnection connection, DbConnectionInterceptionContext<string> interceptionContext)
 {
 }
 /// <summary>
 /// 在打开数据库连接动作执行前瞬间触发。
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="interceptionContext"></param>
 public virtual void Opening(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
 }
 public void DatabaseGetting(DbConnection connection, DbConnectionInterceptionContext<string> interceptionContext)
 {
 }
 /// <summary>
 /// 在获取数据库版本信息动作执行前瞬间触发。
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="interceptionContext"></param>
 public virtual void ServerVersionGetting(DbConnection connection, DbConnectionInterceptionContext<string> interceptionContext)
 {
 }
 public void Disposing(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
     Assert.True(IsClonedEntityConnectionDisposed, "EntityConnection should be disposed of before underlying store connection.");
 }
 /// <summary>
 /// 在获取数据库连接状态动作执行前瞬间触发。
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="interceptionContext"></param>
 public virtual void StateGetting(DbConnection connection, DbConnectionInterceptionContext<ConnectionState> interceptionContext)
 {
 }
 public void Opened(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
 }
 /// <summary>
 /// 在关闭数据库连接动作完成后瞬间触发。
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="interceptionContext"></param>
 public virtual void Closed(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
 }
 public void StateGot(DbConnection connection, DbConnectionInterceptionContext<ConnectionState> interceptionContext)
 {
 }
 /// <summary>
 /// Does not write to log unless overridden.
 /// </summary>
 /// <param name="connection">The connection being closed.</param>
 /// <param name="interceptionContext">Contextual information associated with the call.</param>
 public virtual void Closing(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
 {
 }