Skip to content

Commit f0367d8

Browse files
committed
Minor refactoring
1 parent d542f70 commit f0367d8

File tree

7 files changed

+93
-228
lines changed

7 files changed

+93
-228
lines changed

src/Abstracts/IUnityContainerAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public interface IUnityContainerAsync : IDisposable
9595
/// <param name="name">Name of the object to retrieve.</param>
9696
/// <param name="overrides">Any overrides for the resolve call.</param>
9797
/// <returns>The retrieved object.</returns>
98-
ValueTask<object> Resolve(Type type, string name, params ResolverOverride[] overrides);
98+
ValueTask<object> ResolveAsync(Type type, string name, params ResolverOverride[] overrides);
9999

100100

101101
/// <summary>

src/Extensions/Container/UnityContainerAsync.cs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Runtime.CompilerServices;
5+
using System.Security;
56
using System.Threading.Tasks;
67
using Unity.Injection;
78
using Unity.Lifetime;
@@ -801,10 +802,28 @@ public static IUnityContainerAsync RegisterFactory(this IUnityContainerAsync con
801802
/// <param name="overrides">Any overrides for the resolve call.</param>
802803
/// <returns>The retrieved object.</returns>
803804
[MethodImpl(MethodImplOptions.AggressiveInlining)]
804-
public static ValueTask<object> Resolve<T>(this IUnityContainerAsync container, params ResolverOverride[] overrides)
805+
[SecuritySafeCritical]
806+
public static T ResolveAsync<T>(this IUnityContainerAsync container, params ResolverOverride[] overrides)
805807
{
806-
return (container ?? throw new ArgumentNullException(nameof(container)))
807-
.Resolve(typeof(T), (string)null, overrides);
808+
var unity = container ?? throw new ArgumentNullException(nameof(container));
809+
var value = unity.ResolveAsync(typeof(T), null, overrides);
810+
811+
if (value.IsCompleted) return (T) value.Result;
812+
813+
try
814+
{
815+
var task = value.AsTask();
816+
task.Wait();
817+
818+
return (T) task.Result;
819+
}
820+
catch (AggregateException ex)
821+
{
822+
if (1 == ex.InnerExceptions.Count)
823+
throw ex.InnerException;
824+
825+
throw;
826+
}
808827
}
809828

810829
/// <summary>
@@ -816,10 +835,28 @@ public static ValueTask<object> Resolve<T>(this IUnityContainerAsync container,
816835
/// <param name="overrides">Any overrides for the resolve call.</param>
817836
/// <returns>The retrieved object.</returns>
818837
[MethodImpl(MethodImplOptions.AggressiveInlining)]
819-
public static ValueTask<object> Resolve<T>(this IUnityContainerAsync container, string name, params ResolverOverride[] overrides)
838+
[SecuritySafeCritical]
839+
public static T ResolveAsync<T>(this IUnityContainerAsync container, string name, params ResolverOverride[] overrides)
820840
{
821-
return (container ?? throw new ArgumentNullException(nameof(container)))
822-
.Resolve(typeof(T), name, overrides);
841+
var unity = container ?? throw new ArgumentNullException(nameof(container));
842+
var value = unity.ResolveAsync(typeof(T), name, overrides);
843+
844+
if (value.IsCompleted) return (T)value.Result;
845+
846+
try
847+
{
848+
var task = value.AsTask();
849+
task.Wait();
850+
851+
return (T)task.Result;
852+
}
853+
catch (AggregateException ex)
854+
{
855+
if (1 == ex.InnerExceptions.Count)
856+
throw ex.InnerException;
857+
858+
throw;
859+
}
823860
}
824861

825862
/// <summary>
@@ -830,9 +867,10 @@ public static ValueTask<object> Resolve<T>(this IUnityContainerAsync container,
830867
/// <param name="overrides">Any overrides for the resolve call.</param>
831868
/// <returns>The retrieved object.</returns>
832869
[MethodImpl(MethodImplOptions.AggressiveInlining)]
833-
public static ValueTask<object> Resolve(this IUnityContainerAsync container, Type t, params ResolverOverride[] overrides)
870+
[SecuritySafeCritical]
871+
public static ValueTask<object> ResolveAsync(this IUnityContainerAsync container, Type t, params ResolverOverride[] overrides)
834872
{
835-
return (container ?? throw new ArgumentNullException(nameof(container))).Resolve(t, (string)null, overrides);
873+
return (container ?? throw new ArgumentNullException(nameof(container))).ResolveAsync(t, null, overrides);
836874
}
837875

838876
#endregion

src/Lifetime/Abstracts/ILifetimeManagerAsync.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Lifetime/Abstracts/SynchronizedLifetimeManager.cs

Lines changed: 28 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Threading;
3-
using System.Threading.Tasks;
43

54
namespace Unity.Lifetime
65
{
@@ -25,73 +24,33 @@ public abstract class SynchronizedLifetimeManager : LifetimeManager, IDisposable
2524
{
2625
#region Fields
2726

27+
private readonly object _lock = new object();
28+
2829
/// <summary>
29-
/// This field controls how long the monitor will wait to
30+
/// This field controlls how long the monitor will wait to
3031
/// enter the lock. It is <see cref="Timeout.Infinite"/> by default or number of
3132
/// milliseconds from 0 to 2147483647.
3233
/// </summary>
3334
public static int ResolveTimeout = Timeout.Infinite;
3435

3536
#endregion
3637

37-
38-
#region Constructors
39-
40-
public SynchronizedLifetimeManager()
41-
{
42-
GetResult = GetValue;
43-
SetResult = SetValue;
44-
45-
GetTask = (c) => throw new NotImplementedException();
46-
SetTask = (t, c) => throw new NotImplementedException();
47-
}
48-
49-
#endregion
50-
51-
52-
#region ILifetimeManagerAsync
53-
54-
public Func<ILifetimeContainer, object> GetResult { get; protected set; }
55-
56-
public Action<object, ILifetimeContainer> SetResult { get; protected set; }
57-
58-
public Func<ILifetimeContainer, Task<object>> GetTask { get; protected set; }
59-
60-
public Func<Task<object>, ILifetimeContainer, Task<object>> SetTask { get; protected set; }
61-
62-
#endregion
63-
64-
65-
#region LifetimeManager
66-
6738
/// <inheritdoc/>
6839
public override object GetValue(ILifetimeContainer container = null)
6940
{
70-
if (Monitor.TryEnter(SyncRoot, ResolveTimeout))
41+
if (Monitor.TryEnter(_lock, ResolveTimeout))
7142
{
7243
var result = SynchronizedGetValue(container);
7344
if (NoValue != result)
7445
{
75-
Monitor.Exit(SyncRoot);
46+
Monitor.Exit(_lock);
7647
}
7748
return result;
7849
}
7950
else
8051
throw new TimeoutException($"Failed to enter a monitor");
8152
}
8253

83-
/// <inheritdoc/>
84-
public override void SetValue(object newValue, ILifetimeContainer container = null)
85-
{
86-
SynchronizedSetValue(newValue, container);
87-
TryExit();
88-
}
89-
90-
#endregion
91-
92-
93-
#region SynchronizedLifetimeManager
94-
9554
/// <summary>
9655
/// Performs the actual retrieval of a value from the backing store associated
9756
/// with this Lifetime policy.
@@ -102,6 +61,14 @@ public override void SetValue(object newValue, ILifetimeContainer container = nu
10261
/// <returns>the object desired, or null if no such object is currently stored.</returns>
10362
protected abstract object SynchronizedGetValue(ILifetimeContainer container);
10463

64+
65+
/// <inheritdoc/>
66+
public override void SetValue(object newValue, ILifetimeContainer container = null)
67+
{
68+
SynchronizedSetValue(newValue, container);
69+
TryExit();
70+
}
71+
10572
/// <summary>
10673
/// Performs the actual storage of the given value into backing store for retrieval later.
10774
/// </summary>
@@ -125,41 +92,34 @@ public void Recover()
12592
TryExit();
12693
}
12794

128-
#endregion
129-
130-
131-
#region IDisposable
132-
133-
/// <summary>
134-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
135-
/// </summary>
136-
public void Dispose()
137-
{
138-
Dispose(true);
139-
GC.SuppressFinalize(this);
140-
}
141-
142-
#endregion
143-
144-
145-
#region Implementation
146-
14795
protected virtual void TryExit()
14896
{
14997
#if !NET40
15098
// Prevent first chance exception when abandoning a lock that has not been entered
151-
if (!Monitor.IsEntered(SyncRoot)) return;
99+
if (!Monitor.IsEntered(_lock)) return;
152100
#endif
153101
try
154102
{
155-
Monitor.Exit(SyncRoot);
103+
Monitor.Exit(_lock);
156104
}
157105
catch (SynchronizationLockException)
158106
{
159-
// Ignore.
107+
// Noop here - we don't hold the lock and that's ok.
160108
}
161109
}
162110

111+
112+
#region IDisposable
113+
114+
/// <summary>
115+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
116+
/// </summary>
117+
public void Dispose()
118+
{
119+
Dispose(true);
120+
GC.SuppressFinalize(this);
121+
}
122+
163123
/// <summary>
164124
/// Standard Dispose pattern implementation.
165125
/// </summary>

0 commit comments

Comments
 (0)