Mr.Gomer
Mr.Gomer

Reputation: 649

What does [FromServices] as in parameter imply?

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.

Upvotes: 0

Views: 369

Answers (1)

Camilo Terevinto
Camilo Terevinto

Reputation: 32068

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).

Upvotes: 3

Related Questions