In an ASP.NET MVC application, I'm calling "Services" methods from my controllers.
Usually, a controller named ReportingController
calls methods from ReportingServices
. The service classes are being instantiated with Autofac
with the Mvc.Integration
.
builder.RegisterControllers(Assembly.GetExecutingAssembly());
The service is then injected into the controller constructor.
public ReportingController(ReportingServices service)
So far so good. But occasionally, a controller needs to call methods from other services. I changed the autofac configuration:
builder.RegisterControllers(Assembly.GetExecutingAssembly())
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues);
And added properties to the controllers:
public CommonServices CommonService { get; set; } // new properties
public ReportingController(ReportingServices service) {} // existing ctor
What happens now however is that when the controller is instantiated, all properties are also being set, even if they are never used by a particular ActionMethod.
How can I tell Autofac to delay instantiation of the properties until required or perhaps I should simply not care about this unnecessary initialization?