Created
January 7, 2011 23:28
-
-
Save nblumhardt/770306 to your computer and use it in GitHub Desktop.
Disable disposable resource tracking for a service in Autofac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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