Home > Net > AutoFac injection framework, can't get in the controller constructor injection objects
AutoFac injection framework, can't get in the controller constructor injection objects
Time:10-07
AutoFac injection framework Injection of entity object is used to retrieve component in the key to distinguish between different injection objects, but can't be used in the controller structure method [KeyFilter] inject object for the specified key, but it can be through the controller code to invoke the service layer, in the service layer construction method can through [KeyFilter] inject object for the specified key, is this why?
Startup. Cs in the part of the code is as follows:
//containerBuilder autofac injection container //RedisHelper is a common class, get and set methods of database operation, //in the Startup. In cs injection objects ContainerBuilder. RegisterInstance (new RedisHelper (0, "192.168.99.100:6379", "API")). Keyed (" apiredis ");
Couldn't get into such objects in contraoller,
Public class ValuesController: BaseController { Private readonly RedisHelper _redisHelper;
Public class ValuesController: BaseController { Private readonly IValueService _valueServer;
Public ValuesController ( IValueService valueService) { _valueServer=valueService; } }
Public class ValueService: IValueService { Private readonly RedisHelper _redisHelper; Public ValueService ([KeyFilter (" apiredis ")] RedisHelper RedisHelper) { //this can? Why? _redisHelper=redisHelper; } }
Hope someone told?
CodePudding user response:
Injection is finished in class structure constructor execution is equivalent to the class is constructed so that in the constructor object is empty
CodePudding user response:
In the Startup AddMvc () followed by AddControllersAsServices () :
services. AddMvc (.) AddControllersAsServices ();
CodePudding user response:
Another way, you can in the service layer construction method through [KeyFilter] access to specify the key into the object, using the core asp.net or asp.net?