Skip to content
4 changes: 2 additions & 2 deletions package.props
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project>

<PropertyGroup>
<VersionBase>5.11.4</VersionBase>
<VersionBase>5.11.7</VersionBase>
<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>
</PropertyGroup>

<PropertyGroup>
<UnityAbstractionsVersion>5.11.2</UnityAbstractionsVersion>
<UnityAbstractionsVersion>5.11.*</UnityAbstractionsVersion>
<TargetFrameworks>netstandard2.0;netstandard1.0;netcoreapp2.0;netcoreapp1.0;net47;net46;net45;net40</TargetFrameworks>
</PropertyGroup>

Expand Down
10 changes: 10 additions & 0 deletions src/Extensions/Diagnostic.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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));
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/Factories/EnumerableResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

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


internal static object DiagnosticResolver<TElement>(ref BuilderContext context)
{
return ((UnityContainer)context.Container).ResolveEnumerable<TElement>(context.Resolve,
context.Name).ToArray();
}

internal static ResolveDelegate<BuilderContext> DiagnosticResolverFactory<TElement>()
{
Type type = typeof(TElement).GetGenericTypeDefinition();
return (ref BuilderContext c) => ((UnityContainer)c.Container).ResolveEnumerable<TElement>(c.Resolve, type, c.Name).ToArray();
}

#endregion


Expand Down
2 changes: 1 addition & 1 deletion src/Processors/Constructor/ConstructorResolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected override ResolveDelegate<BuilderContext> 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);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Strategies/LifetimeStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/UnityContainer.Diagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial class UnityContainer

private static Func<Exception, string> 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)
Expand Down
2 changes: 0 additions & 2 deletions src/UnityContainer.IUnityContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ bool IUnityContainer.IsRegistered(Type type, string name) => ReferenceEquals(All
/// <inheritdoc />
object IUnityContainer.Resolve(Type type, string name, params ResolverOverride[] overrides)
{
var n = type.FullName;

// Verify arguments
if (null == type) throw new ArgumentNullException(nameof(type));

Expand Down
2 changes: 1 addition & 1 deletion src/UnityContainer.Implementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions src/UnityContainer.Resolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ private IPolicySet CreateRegistration(Type type, Type policyInterface, object po
#endregion



#region Resolving Enumerable

internal IEnumerable<TElement> ResolveEnumerable<TElement>(Func<Type, string, InternalRegistration, object> resolve, string name)
Expand Down Expand Up @@ -217,6 +216,7 @@ private static IList<TElement> ResolveRegistrations<TElement>(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
Expand Down Expand Up @@ -287,8 +287,11 @@ private static ResolveDelegate<BuilderContext> 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<Expression>();
foreach (var processor in chain)
Expand Down Expand Up @@ -374,7 +377,8 @@ internal ResolveDelegate<BuilderContext> 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);
Expand Down
94 changes: 7 additions & 87 deletions tests/Unity.Diagnostic/Issues.cs
Original file line number Diff line number Diff line change
@@ -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<Diagnostic>();
}
}

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<Diagnostic>();
}
}
}
23 changes: 23 additions & 0 deletions tests/Unity.Specification/Issues.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
15 changes: 0 additions & 15 deletions tests/Unity.Specification/Issues/CodePlex.cs

This file was deleted.

15 changes: 0 additions & 15 deletions tests/Unity.Specification/Issues/GitHub.cs

This file was deleted.