Understanding Dependency Injection in .NET Core

Learn what Dependency Injection and IoC are and what .NET Core provides you to support them.

Brought to you by @andrea.chiarelli

Read on

Please let us know if you have any questions on this front!

Thank you for reading this article. Please, share your thoughts on using dependency injection in .NET Core.

1 Like

Andrea thanks so much for you Article !!! was a great, simple & excellent explanation…
:+1:t2: :+1:t2: :+1:t2:

3 Likes

Glad you found the blog article useful @victorbustos!

Thank you so much, @victorbustos!!! :pray:t2:

What a great explanation! I’d read a few other articles on DI in .NET Core.
This is the first one I could really follow, so clear. Thank you.

2 Likes

Hey @ncam,
Thank you so much for your appreciation! :pray:

1 Like

Thank you so much for this clear explanation! It was a struggle with other articles. I am glad I found this community.

2 Likes

No worries! We’re here for you!

Welcome aboard, @andreelkhoury! :wave:

@andrea.chiarelli thanks for the excellent article! I have read the official docs several times and viewed a dozen other articles on the topic, but I found yours to be the most helpful to me in learning how DI actually works in .NET.

I am curious - is this information still current as of .NET 6? For example, in the Console Application section, you make reference to IServiceProvider and load orderManager via serviceProvider’s GetService method. Is this still the way this is done in .NET 6? I have seen other examples without this code, but I also don’t understand how the service is actually being injected in those examples.

If you want to use DI in console applications, I’d recommend using the worker service template, see:

By living off the generic host, you end up designing applications very similar to ASP.NET Core. New’ing up a service provider manually in an empty console application is not something you’d usually do (IMO), so the blog post is a bit outdated in that respect.

When using the generic host, how do I access services that I implemented as IHostedService in another class?

Hey @chrisxfire,
Welcome to the Auth0 Community and thank you for appreciating my article :slightly_smiling_face:

Actually, the code may be a bit outdated since it uses .NET 3.0. However, the general concepts are still valid.
Regarding using this code in .NET 6, it should still be working. As far as I know, .NET 6 introduced many simplifications from the syntactic point of view, so you may have seen one of these simplified code versions.

Honestly, I’m more used to working with web applications than with console or desktop apps, so I can’t suggest what are the details of the new approach right now.
I suggest taking a look at Microsoft’s general guidelines for dependency injection. Maybe they can help.

Out of curiosity, @Hawxy. Do you suggest the Worker Service template even if I’m not creating a long-running service, such as a typical command line program? :thinking:

Just a note that I’m considering this from the perspective of a beginner with no background in DI.

If you’re writing a command-driven/short-lived console app then no, you probably have no need for dependency injection at all and are better off keeping things very simple, or for more complex CLIs, using Spectre.Console to create a maintainable structure (If you reach the point of requiring DI with Spectre you’re likely not a beginner).

When you need a long-running/timed console application, then the bundle of logging, configuration & familiar DI features that the worker service template/generic host provides makes sense.

I think the blog post shows an uncommon halfway in modern .NET where you’re using a service collection by itself in a simple console app. In my C# circles I’ve seen this usage trip up a lot of new users. An alternative wording of the section might be “How Dependency Injection works in isolation”.

1 Like

Gotcha. Thanks for your suggestions