[Discussion] TestServer no longer preserves ExecutionContext and AsyncLocals between client and server
See original GitHub issueNote: This change affects test code using TestServer only.
Starting in ASP.NET Core 3.0 Preview 6, the ExecutionContext will no longer be preserved when making HTTP requests to TestServer.
As a result of this change, the ExecutionContext in the calling code will not be available to the server code. This means the following values which use ExecutionContext as their underlying storage will not be preserved when performing an HTTP request over TestServer:
Thread.CurrentCultureCultureInfo.CurrentCultureCultureInfo.CurrentUICultureActivity.CurrentThread.CurrentPrincipal- Other values which use
AsyncLocal<T>for storage.
If the previous behavior is required by your test code, you can restore it by setting the PreserveExecutionContext property on TestServer to true:
var server = new TestServer(webHostBuilder)
{
PreserveExecutionContext = true;
};
This is the discussion issue for https://github.com/aspnet/Announcements/issues/358.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:7 (6 by maintainers)
Top Results From Across the Web
TestServer.PreserveExecutionContext Property
Gets or sets a value that controls if ExecutionContext and AsyncLocal<T> values are preserved from the client to the server. The default value...
Read more >Execution context (AsyncLocal) loses values if TestInitialize ...
I know that I can work around this issue by making the test initializer synchronous (and wrap all asynchronous operations in Task.Run(...).
Read more >https://raw.githubusercontent.com/dotnet/samples/m...
In fact, the code above is taken from a service client implementation ... On Linux it is not always possible to preserve symbolic...
Read more >Conveying Context with AsyncLocal | by Norm Bryar
We want our unit-tests to be able to run in parallel. Luckily, this is no different from our web-server running http-requests in parallel:...
Read more >ExecutionContext should be suppressed / LoggerFactory ...
Hi, we are using the .net client (3.2.0). When registered via the DI extensions, this automatically hooks up the ILoggerFactory.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@slang25 Correct, this is about flowing from the client to the server. It’s not a perf thing, it’s a correctness thing. And yeah, it generally makes sense because you are testing an HTTP request, and HTTP requests generally don’t flow
AsyncLocalvalues across them (since they’re cross machine!) 😃.The specific motivation is that flowing execution context actually breaks
IHttpContextAccessorif you have “nested” HTTP calls. Imagine you are testing a multi-app system usingTestServer:Both servers are hosted in
TestServerand you’re giving theHttpClientprovided byTestServertoServer Aso that it can “call”Server B. Testing authentication flows often requires this, as do some microservice systems.During an invocation to
Server A, both servers are sharing theIHttpContextAccessorvalue, which makes all kinds of mess 😃.Thank you for contacting us. Due to a lack of activity on this discussion issue we’re closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn’t been addressed yet, please file a new issue.
This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!