question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Discussion] TestServer no longer preserves ExecutionContext and AsyncLocals between client and server

See original GitHub issue

Note: 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:

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:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
analogrelaycommented, May 10, 2019

@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 AsyncLocal values across them (since they’re cross machine!) 😃.

The specific motivation is that flowing execution context actually breaks IHttpContextAccessor if you have “nested” HTTP calls. Imagine you are testing a multi-app system using TestServer:

Client -> Server A -> Server B

Both servers are hosted in TestServer and you’re giving the HttpClient provided by TestServer to Server A so 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 the IHttpContextAccessor value, which makes all kinds of mess 😃.

0reactions
msftbot[bot]commented, Dec 2, 2020

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found