Last active
February 14, 2019 09:50
-
-
Save chenmach/fe9c029d84f2583dee6837741d5b2296 to your computer and use it in GitHub Desktop.
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
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