-
I've tried to start a new activity UseTestServer(opt =>
{
opt.PreserveExecutionContext = true;
}) so it would be propagated into controller and asp.net core would treat that activity as parent. public class AsyncLocalTest
{
private readonly AsyncLocal<string> _asyncLocalValue = new();
[Before(Test)]
public void Before()
{
_asyncLocalValue.Value = "123";
}
[After(Test)]
public void After()
{
TestContext.Current?.GetDefaultLogger().LogInformation($"Message: {_asyncLocalValue.Value}");
}
[Test]
public async Task Test_method()
{
await Assert.That(_asyncLocalValue.Value).IsEqualTo("123");
}
} I've tried to make Before and After methods synchronous, but that didn't work. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I'm not sure it's possible. AFAIK setting AsyncLocal only propagates from outer to inner / parent to child, and can't do the opposite. Because a Before hook isn't a 'parent' of the main test (more of a sibling I guess) then the async local can't propagate to it? |
Beta Was this translation helpful? Give feedback.
-
Thanks for quick response. I'm looking forward to the new hook method. |
Beta Was this translation helpful? Give feedback.
-
@Laxynium @viceroypenguin could you try v0.4.0 and setting AsyncLocal values in a |
Beta Was this translation helpful? Give feedback.
Option a sounds cleaner and a smoother experience for the end user. Would be a bit of a refactor of code though, but I'll give it a go!