Skip to content

Commit

Permalink
Expose connection string in ConnectionCreating event data (#28563)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers authored Aug 1, 2022
1 parent a946ad5 commit b85a0c9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,28 @@ public class ConnectionCreatingEventData : DbContextEventData
/// <param name="eventDefinition">The event definition.</param>
/// <param name="messageGenerator">A delegate that generates a log message for this event.</param>
/// <param name="context">The <see cref="DbContext" /> currently being used, to null if not known.</param>
/// <param name="connectionString">The connection string for the new connection, if known.</param>
/// <param name="connectionId">A correlation ID that identifies the <see cref="DbConnection" /> instance being used.</param>
/// <param name="startTime">The start time of this event.</param>
public ConnectionCreatingEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
DbContext? context,
string? connectionString,
Guid connectionId,
DateTimeOffset startTime)
: base(eventDefinition, messageGenerator, context)
{
ConnectionString = connectionString;
ConnectionId = connectionId;
StartTime = startTime;
}

/// <summary>
/// The connection string for the new connection, if known.
/// </summary>
public virtual string? ConnectionString { get; }

/// <summary>
/// A correlation ID that identifies the <see cref="DbConnection" /> instance being used.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ public virtual InterceptionResult<DbConnection> ConnectionCreating(

var eventData = BroadcastConnectionCreating(
connection.Context,
connection.ConnectionString,
connection.ConnectionId,
startTime,
definition,
Expand All @@ -939,6 +940,7 @@ public virtual InterceptionResult<DbConnection> ConnectionCreating(

private ConnectionCreatingEventData BroadcastConnectionCreating(
DbContext? context,
string? connectionString,
Guid connectionId,
DateTimeOffset startTime,
EventDefinition definition,
Expand All @@ -949,6 +951,7 @@ private ConnectionCreatingEventData BroadcastConnectionCreating(
definition,
ConnectionCreating,
context,
connectionString,
connectionId,
startTime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public virtual async Task Intercept_connection_creation_passively(bool async)
Assert.False(interceptor.DisposedCalled);
Assert.Same(context, interceptor.Context);
Assert.Same(connection, interceptor.Connection);
Assert.Equal(connection.ConnectionString, interceptor.ConnectionString ?? "");

if (async)
{
Expand Down Expand Up @@ -284,6 +285,7 @@ public virtual async Task Intercept_connection_to_override_creation(bool async)
Assert.False(interceptor.DisposedCalled);
Assert.Same(context, interceptor.Context);
Assert.Same(connection, interceptor.Connection);
Assert.Equal(connection.ConnectionString, interceptor.ConnectionString ?? "");

if (async)
{
Expand Down Expand Up @@ -331,6 +333,7 @@ public virtual async Task Intercept_connection_to_override_connection_after_crea
Assert.False(interceptor.DisposedCalled);
Assert.Same(context, interceptor.Context);
Assert.Same(connection, interceptor.Connection);
Assert.Equal(connection.ConnectionString, interceptor.ConnectionString ?? "");

if (async)
{
Expand Down Expand Up @@ -374,6 +377,7 @@ public virtual async Task Intercept_connection_to_suppress_dispose(bool async)
Assert.False(interceptor.DisposedCalled);
Assert.Same(context, interceptor.Context);
Assert.Same(connection, interceptor.Connection);
Assert.Equal(connection.ConnectionString, interceptor.ConnectionString ?? "");

if (async)
{
Expand Down Expand Up @@ -455,6 +459,7 @@ public virtual async Task Intercept_connection_creation_with_multiple_intercepto
protected class ConnectionCreationInterceptor : IDbConnectionInterceptor
{
public DbContext? Context { get; set; }
public string? ConnectionString { get; set; }
public DbConnection? Connection { get; set; }
public Guid ConnectionId { get; set; }
public DateTimeOffset StartTime { get; set; }
Expand All @@ -474,6 +479,7 @@ public virtual InterceptionResult<DbConnection> ConnectionCreating(
Assert.NotEqual(default, eventData.StartTime);

Context = eventData.Context;
ConnectionString = eventData.ConnectionString;
ConnectionId = eventData.ConnectionId;
StartTime = eventData.StartTime;
CreatingCalled = true;
Expand Down Expand Up @@ -618,7 +624,7 @@ public ConnectionStringContext(Func<DbContextOptionsBuilder, DbContextOptionsBui
_configureProvider = configureProvider;
}

public List<ConnectionCreationInterceptor> Interceptors { get; } = new List<ConnectionCreationInterceptor>();
public List<ConnectionCreationInterceptor> Interceptors { get; } = new();

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> _configureProvider(optionsBuilder).AddInterceptors(Interceptors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ protected override bool ShouldSubscribeToDiagnosticListener
}
}

public class ConnectionInterceptionWithConnectionStringSqlServerTest
: ConnectionInterceptionSqlServerTestBase, IClassFixture<ConnectionInterceptionWithConnectionStringSqlServerTest.InterceptionSqlServerFixture>
{
public ConnectionInterceptionWithConnectionStringSqlServerTest(InterceptionSqlServerFixture fixture)
: base(fixture)
{
}

public class InterceptionSqlServerFixture : InterceptionSqlServerFixtureBase
{
protected override bool ShouldSubscribeToDiagnosticListener
=> false;
}

protected override DbContextOptionsBuilder ConfigureProvider(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer("Database=Dummy");
}

public class ConnectionInterceptionWithDiagnosticsSqlServerTest
: ConnectionInterceptionSqlServerTestBase,
IClassFixture<ConnectionInterceptionWithDiagnosticsSqlServerTest.InterceptionSqlServerFixture>
Expand Down

0 comments on commit b85a0c9

Please sign in to comment.