- Clone the repository to your local workspace.
- Open the
TestApp.csproj
file Visual Studio. This will load the entire project. - Add the details of your Postgres server here.
- Build the project.
- Start Debugging (F5) the project in VS.
What I've tried to demonstrate in this TestApp is that using multiple DbCommandInterceptor
instances in the EFCore pipeline to intercept db requests causes AsyncLocal to lose context.
- Comment out this line which adds
CustomInterceptor_2
to DatabaseContext. - Build and start debugging the application.
- Go to
http://localhost:64116/user
in your browser. - In your Visual Studio, the Debug logs in Output window, you should see the following statements:
Inside ReaderExecutingAsync of CustomInterceptor_1
Setting AsyncLocal value as 'Value_1'
Inside ReaderExecutedAsync of CustomInterceptor_1
Getting AsyncLocalContext Value: 'Value_1'
You can see that AsyncLocal preserves the value across ReaderExecutingAsync
and ReaderExecutedAsync
methods of CustomInterceptor_1
- Uncomment this line to add
CustomInterceptor_2
to the db request pipeline. - Build and start debugging the application.
- Go to
http://localhost:64116/user
in your browser. - In your Visual Studio, the Debug logs in Output window, you should see the following statements:
Inside ReaderExecutingAsync of CustomInterceptor_1
Setting AsyncLocal value as 'Value_1'
Inside ReaderExecutingAsync of CustomInterceptor_2
Inside ReaderExecutedAsync of CustomInterceptor_1
Getting AsyncLocalContext Value: ''
Inside ReaderExecutedAsync of CustomInterceptor_2
Note the statement "Getting AsyncLocalContext Value" has received null
from AsyncLocal but Value_1
was set in the ReaderExecutingAsync
of CustomInterceptor_1.
This means that somehow using multiple instances of DbCommandInterceptor in the EFCore call chain causes inconsistency in the AsyncLocal.