Home > Enterprise >  What does [FromServices] as in parameter imply?
What does [FromServices] as in parameter imply?

Time:01-20

Iv recently changed department at work and after working with Java for 5 years I'm suddenly thrown into C# code. Now most of it is similar to Java but there seems to be a lot more DI going on.

The code is part of a ASPNet core project and there is one thing I don't get. The controller class which is about 10 000 lines of code contains many methods with the following "in parameters":

StreamingUploadMapper([FromServices] EsiDecryptClient esiDecryptClient, [FromServices] IStorageHandler storageHandler)

My question is what is this? Is it auto injected (DI) or must I send these params in when I make a request? I also googled FromServices and it seems its used for DI to avoid using "new"? Other than that I don't even really understand what it is for.

Any help would be much appreciated.

CodePudding user response:

My question is what is this? Is it auto injected (DI)

Yes, that's what it is. You use [FromServices] when you want to inject a service only on a particular action, rather than for the entire Controller. On a 10k LOC Controller, that kind of makes sense (though I'm surprised DI is used with such a beast).

  •  Tags:  
  • Related