From 1fe0fb04366f01f5a4e2c77b442bb20f77e7cd3e Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Sun, 5 Jan 2020 17:09:03 -0800 Subject: [PATCH 01/13] Fixed #197 --- src/UnityContainer.Resolution.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/UnityContainer.Resolution.cs b/src/UnityContainer.Resolution.cs index 5558c5bbe..d185942a4 100644 --- a/src/UnityContainer.Resolution.cs +++ b/src/UnityContainer.Resolution.cs @@ -287,8 +287,11 @@ private static ResolveDelegate OptimizingFactory(ref BuilderCont // Check if optimization is required if (0 == Interlocked.Decrement(ref counter)) { +#if NET40 Task.Factory.StartNew(() => { - +#else + Task.Run(() => { +#endif // Compile build plan on worker thread var expressions = new List(); foreach (var processor in chain) From 0b0a0ae6d50250bd7d8db3f20cdb33860a4bbdb8 Mon Sep 17 00:00:00 2001 From: Eugene Sadocoi Date: Sun, 5 Jan 2020 17:18:02 -0800 Subject: [PATCH 02/13] Releasing v5.11.5 --- package.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.props b/package.props index cf3b888e1..14b2cbfec 100644 --- a/package.props +++ b/package.props @@ -1,7 +1,7 @@ - 5.11.4 + 5.11.5 This package is compatible with .NET Standard 1.0 and 2.0, .NET Core 1.0 and 2.0, .NET 4.0, 4.5, 4.6, 4.7 From e8f7d7083eb5625a1ad0d75f0e078798ccfc17f3 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Thu, 2 Apr 2020 15:22:48 -0700 Subject: [PATCH 03/13] Releasing 5.11.5 --- package.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.props b/package.props index cf3b888e1..7bb634359 100644 --- a/package.props +++ b/package.props @@ -1,12 +1,12 @@ - 5.11.4 + 5.11.5 This package is compatible with .NET Standard 1.0 and 2.0, .NET Core 1.0 and 2.0, .NET 4.0, 4.5, 4.6, 4.7 - 5.11.2 + 5.11.* netstandard2.0;netstandard1.0;netcoreapp2.0;netcoreapp1.0;net47;net46;net45;net40 From 90fb90dc6052e512f79059b5424d648d4bf1735c Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Thu, 2 Apr 2020 15:26:02 -0700 Subject: [PATCH 04/13] Releasing 5.11.6 --- package.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.props b/package.props index 7bb634359..35b52a80f 100644 --- a/package.props +++ b/package.props @@ -1,7 +1,7 @@ - 5.11.5 + 5.11.6 This package is compatible with .NET Standard 1.0 and 2.0, .NET Core 1.0 and 2.0, .NET 4.0, 4.5, 4.6, 4.7 From 884a6c236551057d47ce6328881ea51e72d30840 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Tue, 21 Apr 2020 15:40:52 -0700 Subject: [PATCH 05/13] wip unitycontainer/unity#306 --- src/UnityContainer.Resolution.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/UnityContainer.Resolution.cs b/src/UnityContainer.Resolution.cs index d185942a4..3c2a0e0ba 100644 --- a/src/UnityContainer.Resolution.cs +++ b/src/UnityContainer.Resolution.cs @@ -217,6 +217,7 @@ private static IList ResolveRegistrations(ref BuilderContext else list.Add((TElement)context.Resolve(type, entry.Name, entry.Registration)); } + catch (MakeGenericTypeFailedException) { /* Ignore */ } catch (ArgumentException ex) when (ex.InnerException is TypeLoadException) { // Ignore From dee62c7f301c6b708d7272976bbedc835f26db14 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Tue, 21 Apr 2020 16:29:33 -0700 Subject: [PATCH 06/13] Refactoring and adding tests --- tests/Unity.Diagnostic/Issues.cs | 94 ++------------------ tests/Unity.Specification/Issues.cs | 23 +++++ tests/Unity.Specification/Issues/CodePlex.cs | 15 ---- tests/Unity.Specification/Issues/GitHub.cs | 15 ---- 4 files changed, 30 insertions(+), 117 deletions(-) create mode 100644 tests/Unity.Specification/Issues.cs delete mode 100644 tests/Unity.Specification/Issues/CodePlex.cs delete mode 100644 tests/Unity.Specification/Issues/GitHub.cs diff --git a/tests/Unity.Diagnostic/Issues.cs b/tests/Unity.Diagnostic/Issues.cs index d00b999d6..4f25c8d1f 100644 --- a/tests/Unity.Diagnostic/Issues.cs +++ b/tests/Unity.Diagnostic/Issues.cs @@ -1,103 +1,23 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; using Unity; -using Unity.Builder; -using Unity.Extension; -using Unity.Specification.Diagnostic.Issues.GitHub; -using Unity.Strategies; -namespace GitHub +namespace Issues { [TestClass] - public class Container : Unity.Specification.Diagnostic.Issues.GitHub.SpecificationTests + public class GitHub : Unity.Specification.Diagnostic.Issues.GitHub.SpecificationTests { public override IUnityContainer GetContainer() { - return new UnityContainer().AddExtension(new ForceCompillation()) - .AddExtension(new Diagnostic()) - .AddExtension(new SpyExtension(new SpyStrategy(), UnityBuildStage.Initialization)); + return new UnityContainer().AddNewExtension(); } } - internal class SpyExtension : UnityContainerExtension - { - private BuilderStrategy strategy; - private UnityBuildStage stage; - private object policy; - private Type policyType; - - public SpyExtension(BuilderStrategy strategy, UnityBuildStage stage) - { - this.strategy = strategy; - this.stage = stage; - } - - public SpyExtension(BuilderStrategy strategy, UnityBuildStage stage, object policy, Type policyType) - { - this.strategy = strategy; - this.stage = stage; - this.policy = policy; - this.policyType = policyType; - } - - protected override void Initialize() - { - Context.Strategies.Add(this.strategy, this.stage); - - if (this.policy != null) - { - Context.Policies.Set(null, null, this.policyType, this.policy); - } - } - } - - internal class SpyStrategy : BuilderStrategy - { - private object existing = null; - private bool buildUpWasCalled = false; - - public override void PreBuildUp(ref BuilderContext context) - { - this.buildUpWasCalled = true; - this.existing = context.Existing; - - this.UpdateSpyPolicy(ref context); - } - - public override void PostBuildUp(ref BuilderContext context) - { - this.existing = context.Existing; - } - - public object Existing - { - get { return this.existing; } - } - - public bool BuildUpWasCalled - { - get { return this.buildUpWasCalled; } - } - - private void UpdateSpyPolicy(ref BuilderContext context) - { - SpyPolicy policy = (SpyPolicy)context.Get(null, null, typeof(SpyPolicy)); - - if (policy != null) - { - policy.WasSpiedOn = true; - } - } - } - - internal class SpyPolicy + [TestClass] + public class CodePlex : Unity.Specification.Issues.Codeplex.SpecificationTests { - private bool wasSpiedOn; - - public bool WasSpiedOn + public override IUnityContainer GetContainer() { - get { return wasSpiedOn; } - set { wasSpiedOn = value; } + return new UnityContainer().AddNewExtension(); } } } diff --git a/tests/Unity.Specification/Issues.cs b/tests/Unity.Specification/Issues.cs new file mode 100644 index 000000000..31aa7dd99 --- /dev/null +++ b/tests/Unity.Specification/Issues.cs @@ -0,0 +1,23 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Unity; + +namespace Issues +{ + [TestClass] + public class GitHub : Unity.Specification.Issues.GitHub.SpecificationTests + { + public override IUnityContainer GetContainer() + { + return new UnityContainer(); + } + } + + [TestClass] + public class CodePlex : Unity.Specification.Issues.Codeplex.SpecificationTests + { + public override IUnityContainer GetContainer() + { + return new UnityContainer(); + } + } +} diff --git a/tests/Unity.Specification/Issues/CodePlex.cs b/tests/Unity.Specification/Issues/CodePlex.cs deleted file mode 100644 index e22d1c725..000000000 --- a/tests/Unity.Specification/Issues/CodePlex.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Unity; -using Unity.Specification.Issues.Codeplex; - -namespace Issues -{ - [TestClass] - public class CodePlex : SpecificationTests - { - public override IUnityContainer GetContainer() - { - return new UnityContainer(); - } - } -} diff --git a/tests/Unity.Specification/Issues/GitHub.cs b/tests/Unity.Specification/Issues/GitHub.cs deleted file mode 100644 index d820adaab..000000000 --- a/tests/Unity.Specification/Issues/GitHub.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Unity; -using Unity.Specification.Issues.GitHub; - -namespace Issues -{ - [TestClass] - public class GitHub : SpecificationTests - { - public override IUnityContainer GetContainer() - { - return new UnityContainer(); - } - } -} From 090f47a39ac0667dc289092f03fb4b4f340674a4 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Tue, 21 Apr 2020 17:26:19 -0700 Subject: [PATCH 07/13] Fixes #306 --- src/Extensions/Diagnostic.cs | 10 ++++++++++ src/Factories/EnumerableResolver.cs | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Extensions/Diagnostic.cs b/src/Extensions/Diagnostic.cs index ff0d0f824..38f2fa231 100644 --- a/src/Extensions/Diagnostic.cs +++ b/src/Extensions/Diagnostic.cs @@ -1,6 +1,10 @@ using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Reflection; using Unity.Extension; +using Unity.Factories; +using Unity.Policy; namespace Unity { @@ -44,6 +48,12 @@ protected override void Initialize() { ((UnityContainer)Container).SetDefaultPolicies = UnityContainer.SetDiagnosticPolicies; ((UnityContainer)Container).SetDefaultPolicies((UnityContainer)Container); + + EnumerableResolver.EnumerableMethod = typeof(EnumerableResolver).GetTypeInfo() + .GetDeclaredMethod(nameof(EnumerableResolver.DiagnosticResolver)); + + EnumerableResolver.EnumerableFactory = typeof(EnumerableResolver).GetTypeInfo() + .GetDeclaredMethod(nameof(EnumerableResolver.DiagnosticResolverFactory)); } } diff --git a/src/Factories/EnumerableResolver.cs b/src/Factories/EnumerableResolver.cs index d56286f9f..8dc1a9ea6 100644 --- a/src/Factories/EnumerableResolver.cs +++ b/src/Factories/EnumerableResolver.cs @@ -11,11 +11,11 @@ public class EnumerableResolver { #region Fields - private static readonly MethodInfo EnumerableMethod = + internal static MethodInfo EnumerableMethod = typeof(EnumerableResolver).GetTypeInfo() .GetDeclaredMethod(nameof(EnumerableResolver.Resolver)); - private static readonly MethodInfo EnumerableFactory = + internal static MethodInfo EnumerableFactory = typeof(EnumerableResolver).GetTypeInfo() .GetDeclaredMethod(nameof(EnumerableResolver.ResolverFactory)); @@ -64,6 +64,19 @@ private static ResolveDelegate ResolverFactory() return (ref BuilderContext c) => ((UnityContainer)c.Container).ResolveEnumerable(c.Resolve, type, c.Name); } + + internal static object DiagnosticResolver(ref BuilderContext context) + { + return ((UnityContainer)context.Container).ResolveEnumerable(context.Resolve, + context.Name).ToArray(); + } + + internal static ResolveDelegate DiagnosticResolverFactory() + { + Type type = typeof(TElement).GetGenericTypeDefinition(); + return (ref BuilderContext c) => ((UnityContainer)c.Container).ResolveEnumerable(c.Resolve, type, c.Name).ToArray(); + } + #endregion From e52460897fb8263e6dcdf7e5bfa61d6fc5abe8f6 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Tue, 21 Apr 2020 17:26:19 -0700 Subject: [PATCH 08/13] Fixes unitycontainer/unity#306 --- src/Extensions/Diagnostic.cs | 10 ++++++++++ src/Factories/EnumerableResolver.cs | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Extensions/Diagnostic.cs b/src/Extensions/Diagnostic.cs index ff0d0f824..38f2fa231 100644 --- a/src/Extensions/Diagnostic.cs +++ b/src/Extensions/Diagnostic.cs @@ -1,6 +1,10 @@ using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Reflection; using Unity.Extension; +using Unity.Factories; +using Unity.Policy; namespace Unity { @@ -44,6 +48,12 @@ protected override void Initialize() { ((UnityContainer)Container).SetDefaultPolicies = UnityContainer.SetDiagnosticPolicies; ((UnityContainer)Container).SetDefaultPolicies((UnityContainer)Container); + + EnumerableResolver.EnumerableMethod = typeof(EnumerableResolver).GetTypeInfo() + .GetDeclaredMethod(nameof(EnumerableResolver.DiagnosticResolver)); + + EnumerableResolver.EnumerableFactory = typeof(EnumerableResolver).GetTypeInfo() + .GetDeclaredMethod(nameof(EnumerableResolver.DiagnosticResolverFactory)); } } diff --git a/src/Factories/EnumerableResolver.cs b/src/Factories/EnumerableResolver.cs index d56286f9f..8dc1a9ea6 100644 --- a/src/Factories/EnumerableResolver.cs +++ b/src/Factories/EnumerableResolver.cs @@ -11,11 +11,11 @@ public class EnumerableResolver { #region Fields - private static readonly MethodInfo EnumerableMethod = + internal static MethodInfo EnumerableMethod = typeof(EnumerableResolver).GetTypeInfo() .GetDeclaredMethod(nameof(EnumerableResolver.Resolver)); - private static readonly MethodInfo EnumerableFactory = + internal static MethodInfo EnumerableFactory = typeof(EnumerableResolver).GetTypeInfo() .GetDeclaredMethod(nameof(EnumerableResolver.ResolverFactory)); @@ -64,6 +64,19 @@ private static ResolveDelegate ResolverFactory() return (ref BuilderContext c) => ((UnityContainer)c.Container).ResolveEnumerable(c.Resolve, type, c.Name); } + + internal static object DiagnosticResolver(ref BuilderContext context) + { + return ((UnityContainer)context.Container).ResolveEnumerable(context.Resolve, + context.Name).ToArray(); + } + + internal static ResolveDelegate DiagnosticResolverFactory() + { + Type type = typeof(TElement).GetGenericTypeDefinition(); + return (ref BuilderContext c) => ((UnityContainer)c.Container).ResolveEnumerable(c.Resolve, type, c.Name).ToArray(); + } + #endregion From 5b62003a6e6207f9a125cabfe04180a2db912520 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Tue, 21 Apr 2020 17:40:00 -0700 Subject: [PATCH 09/13] Fixed #204 --- src/UnityContainer.Diagnostic.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UnityContainer.Diagnostic.cs b/src/UnityContainer.Diagnostic.cs index bd14e0828..e776da4b6 100644 --- a/src/UnityContainer.Diagnostic.cs +++ b/src/UnityContainer.Diagnostic.cs @@ -25,7 +25,7 @@ public partial class UnityContainer private static Func CreateMessage = (Exception ex) => { - return $"Resolution failed with error: {ex.Message}\n\nFor more detailed information run Unity in debug mode: new UnityContainer(ModeFlags.Diagnostic)"; + return $"Resolution failed with error: {ex.Message}\n\nFor more detailed information run Unity in debug mode: new UnityContainer().AddExtension(new Diagnostic())"; }; private static string CreateDiagnosticMessage(Exception ex) From 4411cfcaeaf5b6183129fd91b2c35dcebe9cf619 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Wed, 22 Apr 2020 17:39:50 -0700 Subject: [PATCH 10/13] Fixed #210 --- src/Strategies/LifetimeStrategy.cs | 5 ++++- src/UnityContainer.IUnityContainer.cs | 2 -- src/UnityContainer.Resolution.cs | 1 - 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Strategies/LifetimeStrategy.cs b/src/Strategies/LifetimeStrategy.cs index 4fb71767a..5be80d2d6 100644 --- a/src/Strategies/LifetimeStrategy.cs +++ b/src/Strategies/LifetimeStrategy.cs @@ -55,7 +55,10 @@ public override void PreBuildUp(ref BuilderContext context) if (policy is IDisposable) { - context.Lifetime.Add(policy); + var scope = policy is ContainerControlledLifetimeManager container + ? ((UnityContainer)container.Scope)?.LifetimeContainer ?? context.Lifetime + : context.Lifetime; + scope.Add(policy); } } } diff --git a/src/UnityContainer.IUnityContainer.cs b/src/UnityContainer.IUnityContainer.cs index adc7aa5cb..e41486348 100644 --- a/src/UnityContainer.IUnityContainer.cs +++ b/src/UnityContainer.IUnityContainer.cs @@ -233,8 +233,6 @@ bool IUnityContainer.IsRegistered(Type type, string name) => ReferenceEquals(All /// object IUnityContainer.Resolve(Type type, string name, params ResolverOverride[] overrides) { - var n = type.FullName; - // Verify arguments if (null == type) throw new ArgumentNullException(nameof(type)); diff --git a/src/UnityContainer.Resolution.cs b/src/UnityContainer.Resolution.cs index 3c2a0e0ba..e39cef636 100644 --- a/src/UnityContainer.Resolution.cs +++ b/src/UnityContainer.Resolution.cs @@ -85,7 +85,6 @@ private IPolicySet CreateRegistration(Type type, Type policyInterface, object po #endregion - #region Resolving Enumerable internal IEnumerable ResolveEnumerable(Func resolve, string name) From fa926382a5ebf53e3d947214713efa5579154dfc Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Wed, 22 Apr 2020 18:09:47 -0700 Subject: [PATCH 11/13] Fixed #212 --- src/Processors/Constructor/ConstructorResolution.cs | 2 +- src/UnityContainer.Resolution.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Processors/Constructor/ConstructorResolution.cs b/src/Processors/Constructor/ConstructorResolution.cs index 81b07d5c1..bd82d1dce 100644 --- a/src/Processors/Constructor/ConstructorResolution.cs +++ b/src/Processors/Constructor/ConstructorResolution.cs @@ -75,7 +75,7 @@ protected override ResolveDelegate GetResolverDelegate(Construct var dependencies = new object[parameterResolvers.Length]; for (var i = 0; i < dependencies.Length; i++) dependencies[i] = parameterResolvers[i](ref c); - + c.Existing = info.Invoke(dependencies); } diff --git a/src/UnityContainer.Resolution.cs b/src/UnityContainer.Resolution.cs index e39cef636..79af1be76 100644 --- a/src/UnityContainer.Resolution.cs +++ b/src/UnityContainer.Resolution.cs @@ -377,7 +377,8 @@ internal ResolveDelegate ResolvingFactory(ref BuilderContext con !(ex is InvalidRegistrationException) && !(ex is ObjectDisposedException) && !(ex is MemberAccessException) && - !(ex is MakeGenericTypeFailedException)) + !(ex is MakeGenericTypeFailedException) && + !(ex is TargetInvocationException)) throw; throw new ResolutionFailedException(context.RegistrationType, context.Name, CreateMessage(ex), ex); From b5ef9d393879058edf4eae84076af9c3d4e77837 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Wed, 22 Apr 2020 18:33:02 -0700 Subject: [PATCH 12/13] Fixed #177 --- src/UnityContainer.Implementation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UnityContainer.Implementation.cs b/src/UnityContainer.Implementation.cs index 92e7b24e8..edfde6094 100644 --- a/src/UnityContainer.Implementation.cs +++ b/src/UnityContainer.Implementation.cs @@ -112,7 +112,7 @@ private UnityContainer(UnityContainer parent) _isExplicitlyRegistered = _parent._isExplicitlyRegistered; IsTypeExplicitlyRegistered = _parent.IsTypeExplicitlyRegistered; - GetRegistration = _parent.GetRegistration; + GetRegistration = (t, n) => _parent.GetRegistration(t, n); Register = CreateAndSetOrUpdate; GetPolicy = parent.GetPolicy; SetPolicy = CreateAndSetPolicy; From 5539b1a63f0574f224f32e55eb74e8c2ebbec9ec Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Wed, 22 Apr 2020 18:36:37 -0700 Subject: [PATCH 13/13] Releasing 5.11.7 --- package.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.props b/package.props index 35b52a80f..d2fa5eeba 100644 --- a/package.props +++ b/package.props @@ -1,7 +1,7 @@ - 5.11.6 + 5.11.7 This package is compatible with .NET Standard 1.0 and 2.0, .NET Core 1.0 and 2.0, .NET 4.0, 4.5, 4.6, 4.7