From bbf150d39151a0a1395736499f6dd176dc971ea5 Mon Sep 17 00:00:00 2001 From: Bernhard Richter Date: Thu, 29 Dec 2022 23:58:27 +0100 Subject: [PATCH 1/7] Moved TrackInstance to after decorators have been applied --- src/LightInject.Tests/DecoratorTests.cs | 58 +++++++++++++++++++ .../ServiceContainerTests.cs | 2 +- src/LightInject/LightInject.cs | 37 ++++++++---- 3 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/LightInject.Tests/DecoratorTests.cs b/src/LightInject.Tests/DecoratorTests.cs index 60640939..56aff0da 100644 --- a/src/LightInject.Tests/DecoratorTests.cs +++ b/src/LightInject.Tests/DecoratorTests.cs @@ -569,6 +569,49 @@ public void GetInstance_DecoratorWithBaseGenericConstraint_AppliesDecorator() Assert.IsType>(instance); } + [Fact] + public void GetInstance_DecoratorImplementingDisposable_DisposesDecorator() + { + var container = CreateContainer(); + container.Register(new PerScopeLifetime()); + container.Decorate(); + DisposableFooDecorator instance = null; + using (var scope = container.BeginScope()) + { + instance = (DisposableFooDecorator)scope.GetInstance(); + } + + Assert.True(instance.Disposed); + } + + [Fact] + public void GetInstance_DecoratorImplementingDisposableRegisteredAsSingleton_DisposesDecorator() + { + var container = CreateContainer(); + container.Register(new PerContainerLifetime()); + container.Decorate(); + DisposableFooDecorator instance = null; + + + instance = (DisposableFooDecorator)container.GetInstance(); + container.Dispose(); + Assert.True(instance.Disposed); + } + + [Fact] + public void GetInstance_ServicaeAsFactoryAndDecoratorImplementingDisposableRegisteredAsSingleton_DisposesDecorator() + { + var container = CreateContainer(); + container.Register(sf => new DisposableFoo(), new PerContainerLifetime()); + container.Decorate(); + DisposableFooDecorator instance = null; + + + instance = (DisposableFooDecorator)container.GetInstance(); + container.Dispose(); + Assert.True(instance.Disposed); + } + private IFoo CreateFooWithDependency(IServiceFactory factory) { return new FooWithDependency(factory.GetInstance()); @@ -584,4 +627,19 @@ private static FooDecorator GetFooDecorator(IFoo target) return new FooDecorator(target); } } + + + public class DisposableFooDecorator : IFoo, IDisposable + { + public bool Disposed { get; set; } + + public DisposableFooDecorator(IFoo foo) + { + } + + public void Dispose() + { + Disposed = true; + } + } } \ No newline at end of file diff --git a/src/LightInject.Tests/ServiceContainerTests.cs b/src/LightInject.Tests/ServiceContainerTests.cs index 55d761b5..c23d620b 100644 --- a/src/LightInject.Tests/ServiceContainerTests.cs +++ b/src/LightInject.Tests/ServiceContainerTests.cs @@ -1136,7 +1136,7 @@ public void CanGetInstance_KnownService_ReturnsTrue(string serviceName) var canCreateInstance = container.CanGetInstance(typeof(IFoo), serviceName); Assert.True(canCreateInstance); } - + [Theory] [MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))] public void CanGetInstance_UnknownService_ReturnFalse(string serviceName) diff --git a/src/LightInject/LightInject.cs b/src/LightInject/LightInject.cs index 64f3bda3..f4e6ec38 100644 --- a/src/LightInject/LightInject.cs +++ b/src/LightInject/LightInject.cs @@ -3896,18 +3896,18 @@ private void EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter e } } - if (serviceRegistration.Lifetime is PerContainerLifetime && IsNotServiceFactory(serviceRegistration.ServiceType)) - { - var closedGenericTrackInstanceMethod = OpenGenericTrackInstanceMethod.MakeGenericMethod(emitter.StackType); - var containerIndex = constants.Add(this); - emitter.PushConstant(containerIndex, typeof(ServiceContainer)); - emitter.Emit(OpCodes.Call, closedGenericTrackInstanceMethod); - } + // if (serviceRegistration.Lifetime is PerContainerLifetime && IsNotServiceFactory(serviceRegistration.ServiceType)) + // { + // var closedGenericTrackInstanceMethod = OpenGenericTrackInstanceMethod.MakeGenericMethod(emitter.StackType); + // var containerIndex = constants.Add(this); + // emitter.PushConstant(containerIndex, typeof(ServiceContainer)); + // emitter.Emit(OpCodes.Call, closedGenericTrackInstanceMethod); + // } - bool IsNotServiceFactory(Type serviceType) - { - return !typeof(IServiceFactory).GetTypeInfo().IsAssignableFrom(serviceType.GetTypeInfo()); - } + // bool IsNotServiceFactory(Type serviceType) + // { + // return !typeof(IServiceFactory).GetTypeInfo().IsAssignableFrom(serviceType.GetTypeInfo()); + // } } private void EmitDecorators(ServiceRegistration serviceRegistration, IEnumerable serviceDecorators, IEmitter emitter, Action decoratorTargetEmitMethod) @@ -4697,6 +4697,15 @@ private void EmitNewInstanceWithDecorators(ServiceRegistration serviceRegistrati EmitNewInstance(serviceRegistration, emitter); } + if (serviceRegistration.Lifetime is PerContainerLifetime && IsNotServiceFactory(serviceRegistration.ServiceType)) + { + var closedGenericTrackInstanceMethod = OpenGenericTrackInstanceMethod.MakeGenericMethod(emitter.StackType); + var containerIndex = constants.Add(this); + emitter.PushConstant(containerIndex, typeof(ServiceContainer)); + emitter.Emit(OpCodes.Call, closedGenericTrackInstanceMethod); + } + + var processors = initializers.Items.Where(i => i.Predicate(serviceRegistration)).ToArray(); if (processors.Length == 0) { @@ -4723,6 +4732,12 @@ private void EmitNewInstanceWithDecorators(ServiceRegistration serviceRegistrati } emitter.Push(instanceVariable); + + + bool IsNotServiceFactory(Type serviceType) + { + return !typeof(IServiceFactory).GetTypeInfo().IsAssignableFrom(serviceType.GetTypeInfo()); + } } private int GetInstanceDelegateIndex(ServiceRegistration serviceRegistration, Action emitMethod) From b1347d77f91283046272ffe6bb5e08bbb594e741 Mon Sep 17 00:00:00 2001 From: Bernhard Richter Date: Fri, 30 Dec 2022 00:07:00 +0100 Subject: [PATCH 2/7] Use .net7.0 for build --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ebe76511..2014450d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: - name: Install .Net Core uses: actions/setup-dotnet@v2.0.0 with: - dotnet-version: 6.0.400 + dotnet-version: 7.0.100 - name: Install dotnet-script run: dotnet tool install --global dotnet-script - name: Install dotnet-ilverify From eae4835372d64439183570f63f3680ac5bf241c6 Mon Sep 17 00:00:00 2001 From: Bernhard Richter Date: Fri, 30 Dec 2022 00:09:19 +0100 Subject: [PATCH 3/7] Use .Net7.0 in global.json --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index 5e94f4b0..f04f9d06 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "6.0.100", + "version": "7.0.100", "rollForward": "latestFeature" } } \ No newline at end of file From 8b832e77e11310751fad59ff0eaba76bcc31f1a5 Mon Sep 17 00:00:00 2001 From: Bernhard Richter Date: Fri, 30 Dec 2022 00:15:41 +0100 Subject: [PATCH 4/7] Use .net7 in test project --- src/LightInject.Tests/LightInject.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LightInject.Tests/LightInject.Tests.csproj b/src/LightInject.Tests/LightInject.Tests.csproj index 0a6cbfc5..158b02cc 100644 --- a/src/LightInject.Tests/LightInject.Tests.csproj +++ b/src/LightInject.Tests/LightInject.Tests.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0 $(NoWarn);CS0579 net6.0;netstandard2.0 net6.0 From 917a0cac4a810044c87acf78111d1cbac3a8563c Mon Sep 17 00:00:00 2001 From: Bernhard Richter Date: Fri, 30 Dec 2022 00:26:53 +0100 Subject: [PATCH 5/7] Install both 6.0 and 7.0 --- .github/workflows/main.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2014450d..a525a6e0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,9 @@ jobs: - name: Install .Net Core uses: actions/setup-dotnet@v2.0.0 with: - dotnet-version: 7.0.100 + dotnet-version: | + 6.0.x + 7.0.x - name: Install dotnet-script run: dotnet tool install --global dotnet-script - name: Install dotnet-ilverify From cdd1b10897732c4fd049858e297fc93c8fa6447b Mon Sep 17 00:00:00 2001 From: Bernhard Richter Date: Fri, 30 Dec 2022 00:35:46 +0100 Subject: [PATCH 6/7] Use net7.0 in test coverage --- build/build.csx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/build.csx b/build/build.csx index 77d34bd0..42db60d0 100644 --- a/build/build.csx +++ b/build/build.csx @@ -11,7 +11,7 @@ using static ReleaseManagement; [StepDescription("Runs the tests with test coverage")] Step testcoverage = () => { - DotNet.TestWithCodeCoverage(Path.GetDirectoryName(BuildContext.TestProjects[0]), BuildContext.TestCoverageArtifactsFolder, BuildContext.CodeCoverageThreshold, "net6.0"); + DotNet.TestWithCodeCoverage(Path.GetDirectoryName(BuildContext.TestProjects[0]), BuildContext.TestCoverageArtifactsFolder, BuildContext.CodeCoverageThreshold, "net7.0"); }; [StepDescription("Runs all the tests for all target frameworks")] From ac1d8af39b0ccbc42834facf3a1f52fb81847688 Mon Sep 17 00:00:00 2001 From: Bernhard Richter Date: Fri, 30 Dec 2022 00:47:12 +0100 Subject: [PATCH 7/7] Bump version to 6.6.2 --- src/LightInject/LightInject.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LightInject/LightInject.csproj b/src/LightInject/LightInject.csproj index 12a0a56f..c36465c0 100644 --- a/src/LightInject/LightInject.csproj +++ b/src/LightInject/LightInject.csproj @@ -2,7 +2,7 @@ net6.0;netstandard2.0;netcoreapp3.1;net462 - 6.6.1 + 6.6.2 Bernhard Richter https://www.lightinject.net git