Skip to content

Commit

Permalink
Issue #258 - Changed the Scope.Services property to a ConcurrentDicti…
Browse files Browse the repository at this point in the history
…onary to prevent the thread safety race condition from occurring
  • Loading branch information
Ryan Thomas authored and jeremydmiller committed Feb 20, 2021
1 parent b9b6756 commit fd9b664
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Lamar/IoC/Instances/GeneratedInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Func<Scope, object> BuildFuncResolver(Scope scope)
}
service = resolver(s);
s.Services.Add(Hash, service);
s.Services.TryAdd(Hash, service);
return service;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public Func<Scope, object> BuildFuncResolver(Scope scope)
}
service = resolver(root);
root.Services.Add(Hash, service);
root.Services.TryAdd(Hash, service);
return service;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lamar/IoC/Instances/Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Lamar/IoC/Resolvers/ScopedResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Lamar/IoC/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected void assertNotDisposed()

public ConcurrentBag<IDisposable> Disposables { get; } = new ConcurrentBag<IDisposable>();

internal readonly Dictionary<int, object> Services = new Dictionary<int, object>();
internal readonly ConcurrentDictionary<int, object> Services = new ConcurrentDictionary<int, object>();

public virtual void Dispose()
{
Expand Down

0 comments on commit fd9b664

Please sign in to comment.