public class AutofacModule : Module
protected override void Load(ContainerBuilder builder)
builder.RegisterType<MyService>().AsSelf().InstancePerLifetimeScope()
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
Startup.cs
//in ConfigureServices
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(services);
containerBuilder.RegisterModule(new AutofacModule());
var container = containerBuilder.Build();
return new AutofacServiceProvider(container);
SomeOtherService.cs
public class SomeOtherService {
public MyService Service { get; set; } //will remain null when used
As a workaround, you can move the registration into ConfigureServices
and it works ie
//in ConfigureServices
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(services);
containerBuilder.RegisterModule(new AutofacModule());
builder.RegisterType<MyService>().AsSelf().InstancePerLifetimeScope()
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
var container = containerBuilder.Build();
return new AutofacServiceProvider(container);
I am using .NET Core 1.1.2 and Autofac 4.6.2. I can build a reproducible project if that'd be easier.
Shouldn't you use PropertiesAutowired on SomeOtherService since that's the class containing the property you want to autowire? Just made a sample that works ok with ASP.NET Core 2.0 and Autofac 4.7.0 or 4.6.2. I don't have Core 1.1 installed. What version of Autofac.Extensions.DependencyInjection are you running?
That's an excellent point, I see MyService
getting properties autowired but I don't see where SomeOtherService
is getting its properties set up as autowired. If SomeOtherService
needs its properties populated, that registration also needs PropertiesAutowired()
.
I'm going to close this issue based on that being the likely answer here. If there is a reproduction I can see where this isn't working, I can reopen it after I validate there's something going wrong.