Interface IDbConnectionInterceptor
- Namespace
- Microsoft.EntityFrameworkCore.Diagnostics
- Assembly
- Microsoft.EntityFrameworkCore.Relational.dll
Allows interception of operations on DbConnection.
public interface IDbConnectionInterceptor : IInterceptor
Remarks
Connection interceptors can be used to view, change, or suppress the operation on DbConnection, and to modify the result before it is returned to EF.
Consider inheriting from DbConnectionInterceptor if not implementing all methods.
Use Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.AddInterceptors(Microsoft.EntityFrameworkCore.Diagnostics.IInterceptor[]) to register application interceptors.
Extensions can also register interceptors in the internal service provider. If both injected and application interceptors are found, then the injected interceptors are run in the order that they are resolved from the service provider, and then the application interceptors are run last.
See EF Core interceptors for more information and examples.
Methods
ConnectionClosed(DbConnection, ConnectionEndEventData)
Called just after EF has called Close() in an async context.
void ConnectionClosed(DbConnection connection, ConnectionEndEventData eventData)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEndEventDataContextual information about the connection.
ConnectionClosedAsync(DbConnection, ConnectionEndEventData)
Called just after EF has called CloseAsync().
Task ConnectionClosedAsync(DbConnection connection, ConnectionEndEventData eventData)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEndEventDataContextual information about the connection.
Returns
ConnectionClosing(DbConnection, ConnectionEventData, InterceptionResult)
Called just before EF intends to call Close().
InterceptionResult ConnectionClosing(DbConnection connection, ConnectionEventData eventData, InterceptionResult result)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEventDataContextual information about the connection.
resultInterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
resultvalue passed in.
ConnectionClosingAsync(DbConnection, ConnectionEventData, InterceptionResult)
Called just before EF intends to call CloseAsync() in an async context.
ValueTask<InterceptionResult> ConnectionClosingAsync(DbConnection connection, ConnectionEventData eventData, InterceptionResult result)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEventDataContextual information about the connection.
resultInterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- ValueTask<InterceptionResult>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
resultvalue passed in.
ConnectionCreated(ConnectionCreatedEventData, DbConnection)
Called just after EF creates a DbConnection. This event is not triggered if the application provides the connection to use.
DbConnection ConnectionCreated(ConnectionCreatedEventData eventData, DbConnection result)
Parameters
eventDataConnectionCreatedEventDataContextual information about the connection.
resultDbConnectionThe connection that has been created. This value is typically used as the return value for the implementation of this method.
Returns
- DbConnection
The result that EF will use. An implementation of this method for any interceptor that is not attempting to change the result is to return the
resultvalue passed in.
ConnectionCreating(ConnectionCreatingEventData, InterceptionResult<DbConnection>)
Called just before EF creates a DbConnection. This event is not triggered if the application provides the connection to use.
InterceptionResult<DbConnection> ConnectionCreating(ConnectionCreatingEventData eventData, InterceptionResult<DbConnection> result)
Parameters
eventDataConnectionCreatingEventDataContextual information about the connection.
resultInterceptionResult<DbConnection>Represents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.HasResult set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.SuppressWithResult(`0). This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult<DbConnection>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.HasResult is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.HasResult is true, then EF will suppress the operation it was about to perform and use Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.Result instead. An implementation of this method for any interceptor that is not attempting to change the result should return the
resultvalue passed in.
ConnectionDisposed(DbConnection, ConnectionEndEventData)
Called just after EF has called Dispose() in an async context.
void ConnectionDisposed(DbConnection connection, ConnectionEndEventData eventData)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEndEventDataContextual information about the connection.
ConnectionDisposedAsync(DbConnection, ConnectionEndEventData)
Called just after EF has called DisposeAsync().
Task ConnectionDisposedAsync(DbConnection connection, ConnectionEndEventData eventData)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEndEventDataContextual information about the connection.
Returns
ConnectionDisposing(DbConnection, ConnectionEventData, InterceptionResult)
Called just before EF intends to call Dispose() for the DbConnection.
InterceptionResult ConnectionDisposing(DbConnection connection, ConnectionEventData eventData, InterceptionResult result)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEventDataContextual information about the connection.
resultInterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
resultvalue passed in.
ConnectionDisposingAsync(DbConnection, ConnectionEventData, InterceptionResult)
Called just before EF intends to call DisposeAsync() in an async context.
ValueTask<InterceptionResult> ConnectionDisposingAsync(DbConnection connection, ConnectionEventData eventData, InterceptionResult result)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEventDataContextual information about the connection.
resultInterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- ValueTask<InterceptionResult>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
resultvalue passed in.
ConnectionFailed(DbConnection, ConnectionErrorEventData)
Called when closing of a connection has failed with an exception.
void ConnectionFailed(DbConnection connection, ConnectionErrorEventData eventData)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionErrorEventDataContextual information about the connection.
ConnectionFailedAsync(DbConnection, ConnectionErrorEventData, CancellationToken)
Called when closing of a connection has failed with an exception.
Task ConnectionFailedAsync(DbConnection connection, ConnectionErrorEventData eventData, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionErrorEventDataContextual information about the connection.
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
Exceptions
- OperationCanceledException
If the CancellationToken is canceled.
ConnectionOpened(DbConnection, ConnectionEndEventData)
Called just after EF has called Open().
void ConnectionOpened(DbConnection connection, ConnectionEndEventData eventData)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEndEventDataContextual information about the connection.
ConnectionOpenedAsync(DbConnection, ConnectionEndEventData, CancellationToken)
Called just after EF has called OpenAsync().
Task ConnectionOpenedAsync(DbConnection connection, ConnectionEndEventData eventData, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEndEventDataContextual information about the connection.
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
Exceptions
- OperationCanceledException
If the CancellationToken is canceled.
ConnectionOpening(DbConnection, ConnectionEventData, InterceptionResult)
Called just before EF intends to call Open().
InterceptionResult ConnectionOpening(DbConnection connection, ConnectionEventData eventData, InterceptionResult result)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEventDataContextual information about the connection.
resultInterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
resultvalue passed in.
ConnectionOpeningAsync(DbConnection, ConnectionEventData, InterceptionResult, CancellationToken)
Called just before EF intends to call OpenAsync().
ValueTask<InterceptionResult> ConnectionOpeningAsync(DbConnection connection, ConnectionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
Parameters
connectionDbConnectionThe connection.
eventDataConnectionEventDataContextual information about the connection.
resultInterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- ValueTask<InterceptionResult>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
resultvalue passed in.
Exceptions
- OperationCanceledException
If the CancellationToken is canceled.