public IFoo GetOrCreate(IResolverContext ctx, FactoryDelegate fooFactory, string address) { // fancy ensuring that we have a single new Scope created and stored var scopeEntry = _scopes.GetEntryOrDefault(address); if (scopeEntry == null) { Ref.Swap(ref _scopes, address, (x, a) => x.AddOrKeep(a)); scopeEntry = _scopes.GetEntryOrDefault(address); } lock (scopeEntry) if (scopeEntry.Value == null || scopeEntry.Value.IsDisposed) { scopeEntry.Value = ctx.OpenScope(address); } return(((Func <string, IFoo>)fooFactory(scopeEntry.Value)).Invoke(address)); }