Skip to content

Commit 009be65

Browse files
authored
Merge pull request #207 from unitycontainer/release/5.11
Release/5.11.7
2 parents ffd01ee + 5539b1a commit 009be65

File tree

13 files changed

+71
-130
lines changed

13 files changed

+71
-130
lines changed

package.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project>
22

33
<PropertyGroup>
4-
<VersionBase>5.11.4</VersionBase>
4+
<VersionBase>5.11.7</VersionBase>
55
<PackageReleaseNotes>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</PackageReleaseNotes>
66
</PropertyGroup>
77

88
<PropertyGroup>
9-
<UnityAbstractionsVersion>5.11.2</UnityAbstractionsVersion>
9+
<UnityAbstractionsVersion>5.11.*</UnityAbstractionsVersion>
1010
<TargetFrameworks>netstandard2.0;netstandard1.0;netcoreapp2.0;netcoreapp1.0;net47;net46;net45;net40</TargetFrameworks>
1111
</PropertyGroup>
1212

src/Extensions/Diagnostic.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
4+
using System.Reflection;
35
using Unity.Extension;
6+
using Unity.Factories;
7+
using Unity.Policy;
48

59
namespace Unity
610
{
@@ -44,6 +48,12 @@ protected override void Initialize()
4448
{
4549
((UnityContainer)Container).SetDefaultPolicies = UnityContainer.SetDiagnosticPolicies;
4650
((UnityContainer)Container).SetDefaultPolicies((UnityContainer)Container);
51+
52+
EnumerableResolver.EnumerableMethod = typeof(EnumerableResolver).GetTypeInfo()
53+
.GetDeclaredMethod(nameof(EnumerableResolver.DiagnosticResolver));
54+
55+
EnumerableResolver.EnumerableFactory = typeof(EnumerableResolver).GetTypeInfo()
56+
.GetDeclaredMethod(nameof(EnumerableResolver.DiagnosticResolverFactory));
4757
}
4858
}
4959

src/Factories/EnumerableResolver.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public class EnumerableResolver
1111
{
1212
#region Fields
1313

14-
private static readonly MethodInfo EnumerableMethod =
14+
internal static MethodInfo EnumerableMethod =
1515
typeof(EnumerableResolver).GetTypeInfo()
1616
.GetDeclaredMethod(nameof(EnumerableResolver.Resolver));
1717

18-
private static readonly MethodInfo EnumerableFactory =
18+
internal static MethodInfo EnumerableFactory =
1919
typeof(EnumerableResolver).GetTypeInfo()
2020
.GetDeclaredMethod(nameof(EnumerableResolver.ResolverFactory));
2121

@@ -64,6 +64,19 @@ private static ResolveDelegate<BuilderContext> ResolverFactory<TElement>()
6464
return (ref BuilderContext c) => ((UnityContainer)c.Container).ResolveEnumerable<TElement>(c.Resolve, type, c.Name);
6565
}
6666

67+
68+
internal static object DiagnosticResolver<TElement>(ref BuilderContext context)
69+
{
70+
return ((UnityContainer)context.Container).ResolveEnumerable<TElement>(context.Resolve,
71+
context.Name).ToArray();
72+
}
73+
74+
internal static ResolveDelegate<BuilderContext> DiagnosticResolverFactory<TElement>()
75+
{
76+
Type type = typeof(TElement).GetGenericTypeDefinition();
77+
return (ref BuilderContext c) => ((UnityContainer)c.Container).ResolveEnumerable<TElement>(c.Resolve, type, c.Name).ToArray();
78+
}
79+
6780
#endregion
6881

6982

src/Processors/Constructor/ConstructorResolution.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected override ResolveDelegate<BuilderContext> GetResolverDelegate(Construct
7575
var dependencies = new object[parameterResolvers.Length];
7676
for (var i = 0; i < dependencies.Length; i++)
7777
dependencies[i] = parameterResolvers[i](ref c);
78-
78+
7979
c.Existing = info.Invoke(dependencies);
8080
}
8181

src/Strategies/LifetimeStrategy.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ public override void PreBuildUp(ref BuilderContext context)
5555

5656
if (policy is IDisposable)
5757
{
58-
context.Lifetime.Add(policy);
58+
var scope = policy is ContainerControlledLifetimeManager container
59+
? ((UnityContainer)container.Scope)?.LifetimeContainer ?? context.Lifetime
60+
: context.Lifetime;
61+
scope.Add(policy);
5962
}
6063
}
6164
}

src/UnityContainer.Diagnostic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public partial class UnityContainer
2525

2626
private static Func<Exception, string> CreateMessage = (Exception ex) =>
2727
{
28-
return $"Resolution failed with error: {ex.Message}\n\nFor more detailed information run Unity in debug mode: new UnityContainer(ModeFlags.Diagnostic)";
28+
return $"Resolution failed with error: {ex.Message}\n\nFor more detailed information run Unity in debug mode: new UnityContainer().AddExtension(new Diagnostic())";
2929
};
3030

3131
private static string CreateDiagnosticMessage(Exception ex)

src/UnityContainer.IUnityContainer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ bool IUnityContainer.IsRegistered(Type type, string name) => ReferenceEquals(All
233233
/// <inheritdoc />
234234
object IUnityContainer.Resolve(Type type, string name, params ResolverOverride[] overrides)
235235
{
236-
var n = type.FullName;
237-
238236
// Verify arguments
239237
if (null == type) throw new ArgumentNullException(nameof(type));
240238

src/UnityContainer.Implementation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private UnityContainer(UnityContainer parent)
112112
_isExplicitlyRegistered = _parent._isExplicitlyRegistered;
113113
IsTypeExplicitlyRegistered = _parent.IsTypeExplicitlyRegistered;
114114

115-
GetRegistration = _parent.GetRegistration;
115+
GetRegistration = (t, n) => _parent.GetRegistration(t, n);
116116
Register = CreateAndSetOrUpdate;
117117
GetPolicy = parent.GetPolicy;
118118
SetPolicy = CreateAndSetPolicy;

src/UnityContainer.Resolution.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ private IPolicySet CreateRegistration(Type type, Type policyInterface, object po
8585
#endregion
8686

8787

88-
8988
#region Resolving Enumerable
9089

9190
internal IEnumerable<TElement> ResolveEnumerable<TElement>(Func<Type, string, InternalRegistration, object> resolve, string name)
@@ -217,6 +216,7 @@ private static IList<TElement> ResolveRegistrations<TElement>(ref BuilderContext
217216
else
218217
list.Add((TElement)context.Resolve(type, entry.Name, entry.Registration));
219218
}
219+
catch (MakeGenericTypeFailedException) { /* Ignore */ }
220220
catch (ArgumentException ex) when (ex.InnerException is TypeLoadException)
221221
{
222222
// Ignore
@@ -287,8 +287,11 @@ private static ResolveDelegate<BuilderContext> OptimizingFactory(ref BuilderCont
287287
// Check if optimization is required
288288
if (0 == Interlocked.Decrement(ref counter))
289289
{
290+
#if NET40
290291
Task.Factory.StartNew(() => {
291-
292+
#else
293+
Task.Run(() => {
294+
#endif
292295
// Compile build plan on worker thread
293296
var expressions = new List<Expression>();
294297
foreach (var processor in chain)
@@ -374,7 +377,8 @@ internal ResolveDelegate<BuilderContext> ResolvingFactory(ref BuilderContext con
374377
!(ex is InvalidRegistrationException) &&
375378
!(ex is ObjectDisposedException) &&
376379
!(ex is MemberAccessException) &&
377-
!(ex is MakeGenericTypeFailedException))
380+
!(ex is MakeGenericTypeFailedException) &&
381+
!(ex is TargetInvocationException))
378382
throw;
379383

380384
throw new ResolutionFailedException(context.RegistrationType, context.Name, CreateMessage(ex), ex);

tests/Unity.Diagnostic/Issues.cs

Lines changed: 7 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,23 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using System;
32
using Unity;
4-
using Unity.Builder;
5-
using Unity.Extension;
6-
using Unity.Specification.Diagnostic.Issues.GitHub;
7-
using Unity.Strategies;
83

9-
namespace GitHub
4+
namespace Issues
105
{
116
[TestClass]
12-
public class Container : Unity.Specification.Diagnostic.Issues.GitHub.SpecificationTests
7+
public class GitHub : Unity.Specification.Diagnostic.Issues.GitHub.SpecificationTests
138
{
149
public override IUnityContainer GetContainer()
1510
{
16-
return new UnityContainer().AddExtension(new ForceCompillation())
17-
.AddExtension(new Diagnostic())
18-
.AddExtension(new SpyExtension(new SpyStrategy(), UnityBuildStage.Initialization));
11+
return new UnityContainer().AddNewExtension<Diagnostic>();
1912
}
2013
}
2114

22-
internal class SpyExtension : UnityContainerExtension
23-
{
24-
private BuilderStrategy strategy;
25-
private UnityBuildStage stage;
26-
private object policy;
27-
private Type policyType;
28-
29-
public SpyExtension(BuilderStrategy strategy, UnityBuildStage stage)
30-
{
31-
this.strategy = strategy;
32-
this.stage = stage;
33-
}
34-
35-
public SpyExtension(BuilderStrategy strategy, UnityBuildStage stage, object policy, Type policyType)
36-
{
37-
this.strategy = strategy;
38-
this.stage = stage;
39-
this.policy = policy;
40-
this.policyType = policyType;
41-
}
42-
43-
protected override void Initialize()
44-
{
45-
Context.Strategies.Add(this.strategy, this.stage);
46-
47-
if (this.policy != null)
48-
{
49-
Context.Policies.Set(null, null, this.policyType, this.policy);
50-
}
51-
}
52-
}
53-
54-
internal class SpyStrategy : BuilderStrategy
55-
{
56-
private object existing = null;
57-
private bool buildUpWasCalled = false;
58-
59-
public override void PreBuildUp(ref BuilderContext context)
60-
{
61-
this.buildUpWasCalled = true;
62-
this.existing = context.Existing;
63-
64-
this.UpdateSpyPolicy(ref context);
65-
}
66-
67-
public override void PostBuildUp(ref BuilderContext context)
68-
{
69-
this.existing = context.Existing;
70-
}
71-
72-
public object Existing
73-
{
74-
get { return this.existing; }
75-
}
76-
77-
public bool BuildUpWasCalled
78-
{
79-
get { return this.buildUpWasCalled; }
80-
}
81-
82-
private void UpdateSpyPolicy(ref BuilderContext context)
83-
{
84-
SpyPolicy policy = (SpyPolicy)context.Get(null, null, typeof(SpyPolicy));
85-
86-
if (policy != null)
87-
{
88-
policy.WasSpiedOn = true;
89-
}
90-
}
91-
}
92-
93-
internal class SpyPolicy
15+
[TestClass]
16+
public class CodePlex : Unity.Specification.Issues.Codeplex.SpecificationTests
9417
{
95-
private bool wasSpiedOn;
96-
97-
public bool WasSpiedOn
18+
public override IUnityContainer GetContainer()
9819
{
99-
get { return wasSpiedOn; }
100-
set { wasSpiedOn = value; }
20+
return new UnityContainer().AddNewExtension<Diagnostic>();
10121
}
10222
}
10323
}

0 commit comments

Comments
 (0)