diff --git a/src/Lamar/IoC/Instances/GeneratedInstance.cs b/src/Lamar/IoC/Instances/GeneratedInstance.cs index 405e179b..33c9b45c 100644 --- a/src/Lamar/IoC/Instances/GeneratedInstance.cs +++ b/src/Lamar/IoC/Instances/GeneratedInstance.cs @@ -98,7 +98,7 @@ public Func BuildFuncResolver(Scope scope) } service = resolver(s); - s.Services.Add(Hash, service); + s.Services.TryAdd(Hash, service); return service; } @@ -126,7 +126,7 @@ public Func BuildFuncResolver(Scope scope) } service = resolver(root); - root.Services.Add(Hash, service); + root.Services.TryAdd(Hash, service); return service; } diff --git a/src/Lamar/IoC/Instances/Instance.cs b/src/Lamar/IoC/Instances/Instance.cs index 49de677d..e793a995 100644 --- a/src/Lamar/IoC/Instances/Instance.cs +++ b/src/Lamar/IoC/Instances/Instance.cs @@ -234,9 +234,9 @@ protected bool tryGetService(Scope scope, out object service) return scope.Services.TryGetValue(Hash, out service); } - protected void store(Scope scope, object service) + protected bool store(Scope scope, object service) { - scope.Services.Add(Hash, service); + return scope.Services.TryAdd(Hash, service); } /// diff --git a/src/Lamar/IoC/Resolvers/ScopedResolver.cs b/src/Lamar/IoC/Resolvers/ScopedResolver.cs index 58b6a75c..fdd6370f 100644 --- a/src/Lamar/IoC/Resolvers/ScopedResolver.cs +++ b/src/Lamar/IoC/Resolvers/ScopedResolver.cs @@ -23,7 +23,7 @@ public object Resolve(Scope scope) } service = (T) Build(scope); - scope.Services.Add(Hash, service); + scope.Services.TryAdd(Hash, service); if (service is IDisposable) { diff --git a/src/Lamar/IoC/Scope.cs b/src/Lamar/IoC/Scope.cs index 3c6f8fdb..06b64f57 100644 --- a/src/Lamar/IoC/Scope.cs +++ b/src/Lamar/IoC/Scope.cs @@ -93,7 +93,7 @@ protected void assertNotDisposed() public ConcurrentBag Disposables { get; } = new ConcurrentBag(); - internal readonly Dictionary Services = new Dictionary(); + internal readonly ConcurrentDictionary Services = new ConcurrentDictionary(); public virtual void Dispose() {