Skip to content

Instantly share code, notes, and snippets.

@chenmach
Last active February 14, 2019 09:50
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 chenmach/fe9c029d84f2583dee6837741d5b2296 to your computer and use it in GitHub Desktop.
Save chenmach/fe9c029d84f2583dee6837741d5b2296 to your computer and use it in GitHub Desktop.
using Autofac;
namespace AutofacIssue960
{
class AutofacIssue
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterType<FooFactory>().As<IFooFactory>().SingleInstance();
builder.Register(x => x.Resolve<IFooFactory>().CreateFoo());
builder.RegisterType<Stub>();
var container = builder.Build();
using (var moduleScope = container.BeginLifetimeScope(x =>
{
x.RegisterType<Test>().AsSelf().InstancePerLifetimeScope();
}))
{
var stub = moduleScope.Resolve<Stub>();
using (var syncScope = moduleScope.BeginLifetimeScope(y => y.RegisterInstance(stub).AsSelf().ExternallyOwned()))
{
syncScope.Resolve<Test>();
}
stub = moduleScope.Resolve<Stub>();
}
}
}
interface IFooFactory
{
Foo CreateFoo();
}
class Foo
{
}
class Test
{
public Test(Foo foo) { }
}
class Stub
{
public Stub(Foo foo) { }
}
class FooFactory : IFooFactory
{
public Foo CreateFoo()
{
return new Foo();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment