Skip to content

Commit 15db6b3

Browse files
committed
Added debug information for Scopes
Fixes #993
1 parent ecef0d5 commit 15db6b3

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/SimpleInjector/Container.Common.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,15 @@ namespace SimpleInjector
3838
[DebuggerTypeProxy(typeof(ContainerDebugView))]
3939
public partial class Container : ApiObject, IDisposable
4040
{
41-
internal readonly Dictionary<object, Dictionary<Type, WeakReference>> LifestyleRegistrationCache = new();
41+
private static long Counter;
4242

43-
private static long counter;
43+
internal readonly Dictionary<object, Dictionary<Type, WeakReference>> LifestyleRegistrationCache = new();
44+
internal readonly long ContainerId;
4445

4546
private readonly object locker = new();
4647
private readonly List<IInstanceInitializer> instanceInitializers = new();
4748
private readonly List<ContextualResolveInterceptor> resolveInterceptors = new();
4849

49-
private readonly long containerId;
50-
5150
// Collection of (both conditional and unconditional) instance producers that are explicitly
5251
// registered by the user and implicitly registered through unregistered type resolution.
5352
private readonly Dictionary<Type, IRegistrationEntry> explicitRegistrations = new(64);
@@ -73,7 +72,7 @@ public partial class Container : ApiObject, IDisposable
7372
/// <summary>Initializes a new instance of the <see cref="Container"/> class.</summary>
7473
public Container()
7574
{
76-
this.containerId = Interlocked.Increment(ref counter);
75+
this.ContainerId = Interlocked.Increment(ref Counter);
7776

7877
this.ContainerScope = new ContainerScope(this);
7978

src/SimpleInjector/Scope.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ namespace SimpleInjector
66
using System;
77
using System.Collections;
88
using System.Collections.Generic;
9+
using System.Diagnostics;
910
using System.Runtime.CompilerServices;
11+
using System.Threading;
1012
using System.Threading.Tasks;
1113
using SimpleInjector.Advanced;
1214
using SimpleInjector.Internals;
@@ -52,12 +54,16 @@ public partial class Scope
5254
/// thread-safe methods. This means that once the Scope is ready for disposal, only a single thread should
5355
/// access it. Also note that although the scope is thread safe, cached instances might not be.
5456
/// </remarks>
57+
[DebuggerDisplay("{DebugString,nq}")]
5558
public partial class Scope : ApiObject, IDisposable, IServiceProvider
5659
{
5760
private const int MaximumDisposeRecursion = 100;
5861

62+
private static long counter = 0;
63+
5964
private readonly object syncRoot = new();
6065
private readonly ScopeManager? manager;
66+
private readonly long scopeId = Interlocked.Increment(ref counter);
6167

6268
private IDictionary? items;
6369
private Dictionary<Registration, object>? cachedInstances;
@@ -109,6 +115,11 @@ private enum DisposeState
109115

110116
internal bool IsContainerScope { get; set; }
111117

118+
private string DebugString =>
119+
"Scope #" + this.scopeId
120+
+ (this.ParentScope is not null ? (" for Parent #" + this.ParentScope.scopeId) : "")
121+
+ (this.Container is not null ? (" for Container #" + this.Container!.ContainerId) : "");
122+
112123
/// <summary>Gets an instance of the given <typeparamref name="TService"/> for the current scope.</summary>
113124
/// <typeparam name="TService">The type of the service to resolve.</typeparam>
114125
/// <remarks><b>Thread safety:</b> Calls to this method are thread safe.</remarks>

0 commit comments

Comments
 (0)