Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lock to scoped burdens to ensure only one instance is created per scope #547

Merged
merged 8 commits into from
Sep 15, 2020
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
image: Visual Studio 2017
image: Visual Studio 2019

build:
verbosity: minimal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.2;net461</TargetFrameworks>
<TargetFrameworks>netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net461</TargetFrameworks>

<IsPackable>false</IsPackable>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class ExtensionContainerScope : ILifetimeScope, IDisposable
protected static readonly AsyncLocal<ExtensionContainerScope> current = new AsyncLocal<ExtensionContainerScope>();
private readonly ExtensionContainerScope parent;
private readonly IScopeCache scopeCache;

protected ExtensionContainerScope(ExtensionContainerScope parent)
{
scopeCache = new ScopeCache();
Expand Down Expand Up @@ -63,21 +63,25 @@ public void Dispose()

public Burden GetCachedInstance(ComponentModel model, ScopedInstanceActivationCallback createInstance)
{
if(model.Configuration.Attributes.Get(TransientMarker) == Boolean.TrueString )
{
var burden = createInstance((_) => {});
scopeCache[burden] = burden;
return burden;
}
else
lock (scopeCache)
{
var burden = scopeCache[model];
if (burden == null)

// Add transient's burden to scope so it gets released
if (model.Configuration.Attributes.Get(TransientMarker) == bool.TrueString)
{
scopeCache[model] = burden = createInstance((_) => {});
var transientBurden = createInstance((_) => {});
scopeCache[transientBurden] = transientBurden;
return transientBurden;
}

return burden;
var scopedBurden = scopeCache[model];
if (scopedBurden != null)
{
return scopedBurden;
}
scopedBurden = createInstance((_) => {});
scopeCache[model] = scopedBurden;
return scopedBurden;
}
}

Expand Down