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 ValuesController ([KeyFilter (" apiredis ")] RedisHelper RedisHelper)
{
//why not?
_redisHelper=redisHelper;
}
}


Here by a service layer can.
 
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?

CodePudding user response:

The core asp.net

CodePudding user response:

refer to the second floor ylhyh response:
AddMvc in the Startup () followed by AddControllersAsServices () :

 services. AddMvc (.) AddControllersAsServices (); 

I tested, or no, I don't know who practice it

CodePudding user response:

 containerBuilder. RegisterInstance (new RedisHelper (0, "192.168.99.100:6379", "API")). As (). Keyed (" apiredis "); 

And As you can

CodePudding user response:

After the wrong, plus the As KeyFilter doesn't work

CodePudding user response:

CodePudding user response:

@ black Liu Yan, have a look at this piece of emphatically:
https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#controllers-as-services

CodePudding user response:

I now is Keyed by ApplicationContainer. Sign up to the problem of ResolveKeyed (" RedisCache "); Can get, but not in building function:

Registration:
 builder. RegisterType () 
. Keyed (" RedisCache ")
SingleInstance ();


Take less than in structure:
 
Public TestService ([KeyFilter (" Redis ")] IRedisLib Redis)
{
.
}


ResolveKeyed can:
 ApplicationContainer. ResolveKeyed (" RedisCache "); 

CodePudding user response:

Ignore the above structure of Key do not match, match the also can't take

CodePudding user response:

  there are a lot of strange, I also two eyes become speechless,

CodePudding user response:

knock refer to 12 floor crying response:
  there are a lot of strange, I also am eyes become speechless,



My problem is resolved, I am in web API ValuesController constructor by KeyFilter for instance.

 
Public interface ISay
{
String Say ();
}


 
Public class Foobar: ISay
{
Public Foobar (string name)
{
Name=Name;
}

Public string Name {get; Private set; }

Public string Say ()
{
Return $" My name is {name} ";
}
}


 
Public IServiceProvider ConfigureServices (IServiceCollection services)
{
Services. AddMvc (.) AddControllersAsServices ();

Var builder=new ContainerBuilder ();
Builder. The Populate (services);
Builder. RegisterType (). UsingConstructor (typeof (string)). WithParameter (" name ", "Eric"). Named (" Eric "). SingleInstance ();
Builder. RegisterType nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related