public void Register_Resolve_Scoped() { //ARRANGE var serviceCollection = new ServiceCollection(); serviceCollection.AddScoped <IDisposable, TrackableDisposable>(); var tracker = new Tracker(); var container = new Container(builder => { builder.Register <ITracker>(c => c.Inject(() => tracker)); builder.RegisterServices(serviceCollection); builder.ConfigureSettings(SingularitySettings.Microsoft); }); //ACT int disposeCountBefore; IDisposable instance1, instance2; using (Scoped scope = container.BeginScope()) { instance1 = scope.GetInstance <IDisposable>(); instance2 = scope.GetInstance <IDisposable>(); disposeCountBefore = tracker.DisposeCount; } int disposeCountAfter = tracker.DisposeCount; //ASSERT Assert.Same(instance1, instance2); Assert.IsType <TrackableDisposable>(instance1); Assert.Equal(0, disposeCountBefore); Assert.Equal(1, disposeCountAfter); }
public void BeginScope_GetInstance_AlwaysReturnsSameInstanceForTheSameScope() { //ARRANGE var container = new Container(builder => { builder.Register <IDisposable, Disposable>(c => c .With(Lifetime.PerScope) .WithFinalizer(x => x.Dispose())); }); //ACT Scoped scope1 = container.BeginScope(); var disposable1 = scope1.GetInstance <IDisposable>(); var disposable2 = scope1.GetInstance <IDisposable>(); Scoped scope2 = container.BeginScope(); var disposable3 = scope2.GetInstance <IDisposable>(); var disposable4 = scope2.GetInstance <IDisposable>(); //ASSERT Assert.Same(disposable1, disposable2); Assert.Same(disposable3, disposable4); Assert.NotSame(disposable1, disposable3); }
public void GetInstance_Nested() { //ARRANGE var container = new Container(builder => { builder.Register <ITestService10, TestService10>(c => c .With(Lifetimes.PerScope)); builder.Register <ITestService11, TestService11>(); }); //ACT Scoped scope1 = container.BeginScope(); var testService11 = scope1.GetInstance <ITestService11>(); //ASSERT Assert.IsType <TestService11>(testService11); Assert.IsType <TestService10>(testService11.TestService10); }
public void BeginScope_GetInstance_DisposeScope() { //ARRANGE var container = new Container(builder => { builder.Register <IDisposable, Disposable>(c => c .With(Lifetime.PerScope) .WithFinalizer(x => x.Dispose())); }); //ACT Scoped scope = container.BeginScope(); var disposable = (Disposable)scope.GetInstance <IDisposable>(); bool isDisposedBefore = disposable.IsDisposed; scope.Dispose(); bool isDisposedAfter = disposable.IsDisposed; //ASSERT Assert.False(isDisposedBefore); container.Dispose(); Assert.True(isDisposedAfter); }
/// <inheritdoc /> public object GetService(Type serviceType) { return(_scope.GetInstance(serviceType)); }