Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Understanding C# FromServices

So iv just been thrown into some new code at work and its in C#. Now I’m not very familiar with C# and there are some things I really don’t understand and the main one is Injecting into methods.

Now there is a WebAPI and it has controller that uses a class named "LocalFileStorage" which is a dependency from another project, the constructor for that class looks like this.

 public class LocalFileStorageHandler : IStorageHandler
    {
       *Bunch of private variables

        public LocalFileStorageHandler(DbContext dbContext, IConfiguration configuration, ILogger<LocalFileStorageHandler> logger)
        { code here}

Now in the controller class every method that uses the LocalFileStorage gets it injected as a parameter. Here is a example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public async Task<IActionResult> ApiMapper([FromBody] dynamic request, [FromServices] IStorageHandler storageHandler)
        

Also in the project startup.cs we can find this line:

services.AddScoped<IStorageHandler, LocalFileStorageHandler>();

Now my understanding is that for each separate request made the Addscoped makes sure that the method gets its own instance of LocalFileStorage handler. I also understand that the "[FromServices]" attribute causes this instance to be injected into the method. However the one thing I don’t understand and cant find anywhere in the code is where the LocalFileStorage objects get their "In parameters" for their constructor?

As by my understanding each injected instance of LocalFileStorage should also receive the parameters:

DbContext dbContext, IConfiguration configuration, ILogger<LocalFileStorageHandler> logger 

What am i missing here ?

Kind regards

>Solution :

The DI container injects the dependencies for you. So somewhere, the DbContext, IConfiguration and ILogger has been registered/setup.

Then when you use the FromServices attribute, the DI container will try to resolve the type for you and inject all dependencies (if they are registered, if not, an exception will be thrown)

IConfiguration and ILogger is usually setup when building the host. DbContext are (usually) registered by using the AddDbContext extension method.

Link to configuration for ILogger: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-6.0#logging-providers

Link to configuration for IConfiguration: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#default-configuration

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading