添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This was originally reported in October 2015, here .

When a service is registered within a module via PropertiesAutowired() , the property is not injected. Instead, there are no autofac registration errors, the service is just null.

To reproduce:

AutofacModule.cs

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.