Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created July 15, 2020 23:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dcomartin/93bded60711e73b8e68ad54ad84867b1 to your computer and use it in GitHub Desktop.
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