Skip to content

Commit

Permalink
Fixes DependencyOverride
Browse files Browse the repository at this point in the history
Fixed #257
  • Loading branch information
ENikS committed Nov 5, 2020
1 parent 5d4c424 commit ef0e00f
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/BuiltIn/Processors/Constructor/Constructor.Activation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void Build(ref PipelineContext context, object?[] data)
}
catch (Exception ex)
{
context.Exception(ex);
context.Error(ex.Message);
}
}

Expand All @@ -138,7 +138,7 @@ private void Build(ref PipelineContext context)
}
catch (Exception ex)
{
context.Exception(ex);
context.Error(ex.Message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/BuiltIn/Processors/Factory/Factory.Resolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override void PreBuild(ref PipelineContext context)
}
catch (Exception ex)
{
context.Exception(ex);
context.Capture(ex);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/BuiltIn/Processors/Member/Member.Implementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Build(ref PipelineContext context, in ImportInfo<TMemberInfo> import

local.Target = data.DataType switch
{
ImportType.None => local.Resolve(),
ImportType.None => context.Container.Resolve(ref local),

ImportType.Pipeline => local.GetValueRecursively(import.Element,
((ResolveDelegate<PipelineContext>)data.Value!).Invoke(ref local)),
Expand Down
4 changes: 2 additions & 2 deletions src/Container/Exceptions/ResolutionFailedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public ResolutionFailedException(Type type, string? name, string message, Except
}


public ResolutionFailedException(ref Contract contract, ref RequestInfo info)
: base(info.ErrorInfo.Message, info.ErrorInfo.Exception)
public ResolutionFailedException(in Contract contract, string? message)
: base(message)
{
TypeRequested = contract.Type.Name;
NameRequested = contract.Name;
Expand Down
17 changes: 16 additions & 1 deletion src/Container/Info/ErrorInfo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
using System;
using System.Runtime.ExceptionServices;

namespace Unity.Container
{
public struct ErrorInfo
{
#region Fields

private ExceptionDispatchInfo? _exception;
public bool IsFaulted;
public string? Message;
public Exception? Exception;

#endregion

public void Capture(Exception exception)
{
IsFaulted = true;
Message = exception.Message;

_exception = ExceptionDispatchInfo.Capture(exception);
}

public void Throw() => _exception?.Throw();
}
}
2 changes: 1 addition & 1 deletion src/Container/Pipeline/Action/PipelineAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void Error(string error)
public void Exception(Exception ex)
{
// Report exception
Context.Exception(ex);
Context.Capture(ex);
}

public bool Success(object? data)
Expand Down
8 changes: 2 additions & 6 deletions src/Container/Pipeline/Context/PipelineContext.Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,11 @@ public object Error(string error)
return error;
}

public void Exception(Exception exception)
public void Capture(Exception exception)
{
unsafe
{
ref var info = ref Unsafe.AsRef<ErrorInfo>(_error.ToPointer());

info.Message = exception.Message;
info.Exception = exception;
info.IsFaulted = true;
Unsafe.AsRef<ErrorInfo>(_error.ToPointer()).Capture(exception);
}
}

Expand Down
33 changes: 4 additions & 29 deletions src/Container/Unity/Extensions/Register.Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class UnityRegisterInstanceExtensions
/// <param name="instance">Object to returned.</param>
/// <returns>The <see cref="IUnityContainer"/> object that this method was called on.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, object? instance, params InjectionMember[] injectionMembers)
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, TInterface instance, params InjectionMember[] injectionMembers)
{
if (container is null) throw new ArgumentNullException(nameof(container));

Expand All @@ -58,7 +58,7 @@ public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer
/// <see cref="LifetimeManager"/> object that controls how this instance will be managed by the container.</param>
/// <returns>The <see cref="IUnityContainer"/> object that this method was called on.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, object? instance, IInstanceLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, TInterface instance, IInstanceLifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
if (container is null) throw new ArgumentNullException(nameof(container));

Expand Down Expand Up @@ -88,9 +88,10 @@ public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer
/// <param name="name">Name for registration.</param>
/// <returns>The <see cref="IUnityContainer"/> object that this method was called on.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, string name, object? instance, params InjectionMember[] injectionMembers)
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, string name, TInterface instance, params InjectionMember[] injectionMembers)
{
if (container is null) throw new ArgumentNullException(nameof(container));
if (instance is null) throw new ArgumentNullException(nameof(instance));

return container.Register(new RegistrationDescriptor(instance, name, (IInstanceLifetimeManager)LifetimeManager._instanceManager.Clone(injectionMembers), typeof(TInterface)));
}
Expand Down Expand Up @@ -250,32 +251,6 @@ public static IUnityContainer RegisterInstance(this IUnityContainer container, s
return container.Register(new RegistrationDescriptor(instance, name, lifetimeManager, instance.GetType()));
}

/// <summary>
/// Register an instance with the container.
/// </summary>
/// <remarks>
/// <para>
/// Instance registration is much like setting a type as a singleton, except that instead
/// of the container creating the instance the first time it is requested, the user
/// creates the instance ahead of type and adds that instance to the container.
/// </para>
/// <para>
/// This overload automatically has the container take ownership of the <paramref name="instance"/>.</para>
/// </remarks>
/// <param name="container">Container to configure.</param>
/// <param name="type">Type of instance to register (may be an implemented interface instead of the full type).</param>
/// <param name="instance">Object to returned.</param>
/// <param name="name">Name for registration.</param>
/// <returns>The <see cref="IUnityContainer"/> object that this method was called on.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IUnityContainer RegisterInstance(this IUnityContainer container, string name, object instance, params InjectionMember[] injectionMembers)
{
if (container is null) throw new ArgumentNullException(nameof(container));
if (instance is null) throw new ArgumentNullException(nameof(instance));

return container.Register(new RegistrationDescriptor(instance, name, (IInstanceLifetimeManager)LifetimeManager._instanceManager.Clone(injectionMembers), instance.GetType()));
}

/// <summary>
/// Register an instance with the container.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Container/Unity/Resolution/Unity.Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public partial class UnityContainer
var value = Unsafe.As<LifetimeManager>(manager).GetValue(_scope);
if (!ReferenceEquals(RegistrationManager.NoValue, value)) return value;

context = new PipelineContext(container, ref contract, ref request);
context = new PipelineContext(container, ref contract, manager, ref request);

return ImportSource.Local == manager.Source
? ResolveRegistration(ref context)
Expand Down
7 changes: 5 additions & 2 deletions src/Container/Unity/Resolution/Unity.Registered.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class UnityContainer

// Resolve
context.Target = manager.Pipeline!(ref context);
context.LifetimeManager?.SetValue(context.Target, _scope);
if (!context.IsFaulted) context.LifetimeManager?.SetValue(context.Target, _scope);
}
catch when (manager is SynchronizedLifetimeManager synchronized)
{
Expand All @@ -29,7 +29,10 @@ public partial class UnityContainer
}

if (request.IsFaulted)
throw new ResolutionFailedException(contract.Type, contract.Name, request.ErrorInfo.Message!);
{
request.ErrorInfo.Throw();
throw new ResolutionFailedException(in contract, request.ErrorInfo.Message);
}

return context.Target;
}
Expand Down
18 changes: 6 additions & 12 deletions src/Container/Unity/Resolution/Unity.Unregistered.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@ public partial class UnityContainer
{
private object? ResolveUnregistered(ref Contract contract, ResolverOverride[] overrides)
{
object? value = default;
var request = new RequestInfo(overrides);
var value = ResolveRequest(ref contract, ref request);

try
{
value = ResolveRequest(ref contract, ref request);
}
catch (Exception ex)
{
//if (context.Registration is SynchronizedLifetimeManager manager)
// manager.Recover();
// TODO: Check synchronized disposal

//context.Exception(ex);
if (request.IsFaulted)
{
request.ErrorInfo.Throw();
throw new ResolutionFailedException(in contract, request.ErrorInfo.Message);
}

if (request.IsFaulted) throw new ResolutionFailedException(ref contract, ref request);

return value;
}

Expand Down

0 comments on commit ef0e00f

Please sign in to comment.