Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/Test.cs

Created Jul 15, 2020
Embed
What would you like to do?
public class ScopedLifetimeTest
{
[Fact]
public async Task ShouldShitTheBed()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<ClassA>();
serviceCollection.AddScoped<ClassB>();
serviceCollection.AddScoped<ClassC>();
var provider = serviceCollection.BuildServiceProvider();
var classA = provider.GetService<ClassA>();
var result = await classA.DoWork();
// This will fail because it will return 0.
// This is because ClassB sets the _state member second, but finishes first.
// ClassA finishes last and returns _state.length which is now blank because ClassB has already run.
result.ShouldBe(4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.