Skip to content

Instantly share code, notes, and snippets.

@nblumhardt
Created January 7, 2011 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nblumhardt/770306 to your computer and use it in GitHub Desktop.
Save nblumhardt/770306 to your computer and use it in GitHub Desktop.
Disable disposable resource tracking for a service in Autofac
var builder = new ContainerBuilder();
// The thing we want to create haphazardly without ever
// disposing it or any of its dependencies:
builder.RegisterType<Foo>()
.Named<IFoo>("untracked");
// Owned<T> is itself untracked (i.e. disposal is up to the
// owner.) Since we just let the Owned<IFoo> slip away, it
// will be GC'ed. Owned<T> doesn't implement a finalizer (and
// nor should it) so any IFoos resolved this way will stick around happily
// until they're GC'ed themselves.
builder.Register(c => c.ResolveNamed<Owned<IFoo>>("untracked").Value)
.As<IFoo>();
var container = builder.Build();
var factory = container.Resolve<Func<IFoo>>();
while (true)
var untrackedFoo = factory();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment