I can use Constructor Parameter Injection easily In MVC Core. But Property Injection is not supported.I try use AutoFac but fail too.
So how to use Property Injection in MVC Core.
Here is the code with AutoFac
services.AddMvc();
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<Test2>().As<ITest>();
builder.RegisterType<HomeController>().PropertiesAutowired();
builder.Populate(services);
var container = builder.Build();
//The following code works
HomeController test2 = container.Resolve<HomeController>();
return new AutofacServiceProvider(container);
In dotnet core, you need to make the following changes to make Autofac work: Add a public Autofac
IContainerin your applicationStartup.csChange
ConfigureServicesinStartup.csto returnIServiceProvider, do all your registrations, populate the framework services in your container by usingbuilder.Populat(services);. Please note that there is no need for you to dobuilder.RegisterType<HomeController>().PropertiesAutowired();You will also need to make sure you dispose the container on application stopped by doing this in your
Configuremethod.After you do this - your controllers should automatically get the properties autowired.