Dependency Injection for ASP NET Core 6.0 - An UPDATED 2022 Tutorial
Вставка
- Опубліковано 23 лип 2024
- 🚀 C# Progress Academy - Become a senior C# developer: academy.tutorials.eu/p/csharp...
Dependency Injection in ASP.NET Core 6.0 has been one of our number 1 requests, and now it´s finally here! Want to learn more about C# and ASP.NET?
Get to work with Dependency Injection in an ASP.NET 6 MVC Project!
Here are some links that you may find interesting👀
· Check out the C# Masterclass: bit.ly/38xC6WW
· FREE C# Download 9 MUST have .NET Developer tools: page.tutorials.eu/tools
We´ll make sure to make a Developer out of you In no time!
☕ You can also support us now by buying us a coffee! ☕ www.buymeacoffee.com/tutorialsEU
And we promise to create more cool content for you!
ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies.
For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core.
For information on using dependency injection in applications other than web apps, see Dependency injection in .NET.
For more information on dependency injection of options, see Options pattern in ASP.NET Core.
This topic provides information on dependency injection in ASP.NET Core. The primary documentation on using dependency injection is contained in Dependency injection in .NET.
View or download sample code (how to download)
Overview of dependency injection
A dependency is an object that another object depends on. Examine the following MyDependency class with a WriteMessage method that other classes depend on.
A class can create an instance of the MyDependency class to make use of its WriteMessage method. In the following example, the MyDependency class is a dependency of the IndexModel class.
The class creates and directly depends on the MyDependency class. Code dependencies, such as in the previous example, are problematic and should be avoided for the following reasons:
· To replace MyDependency with a different implementation, the IndexModel class must be modified.
· If MyDependency has dependencies, they must also be configured by the IndexModel class. In a large project with multiple classes depending on MyDependency, the configuration code becomes scattered across the app.
· This implementation is difficult to unit test.
Dependency injection addresses these problems through:
· The use of an interface or base class to abstract the dependency implementation.
· Registration of the dependency in a service container. ASP.NET Core provides a built-in service container, IServiceProvider. Services are typically registered in the app's Program.cs file.
· Injection of the service into the constructor of the class where it's used. The framework takes on the responsibility of creating an instance of the dependency and disposing of it when it's no longer needed.
Interested in this? Watch the video to find out more about ASP NET Dependency Injection!
#asp #dotnet #DependencyInjection #code #programming #csharp
TIMESTAMPS
00:00 Dependency Injection in .NET? Yes please!
00:24 Why dependency Injection?
01:00 Creating a Service
02:32 Register our Service
04:52 Using our Service
07:20 Summary on how to get the DI going
07:58 Thanks for watching!
TAGS
dependency injection in asp.net core,asp.net dependency injection,dependency injection,visual studio,c# dependency injection tutorial,dotnet,dependency injection explained,dependency injection tutorial,csharp,c sharp,di,ioc,tutorial,programming,code,visual studio 2022,asp.net core 6,dotnet 6,asp.net core api,.net core,asp.net core,tutorialseu,asp net,mvc,asp net core,tim corey,asp net mvc,dotnet core,asp net core mvc,freecodecamp,dev,iamtimcorey,how to
tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Stay tuned and subscribe to tutorialsEU: goo.gl/rBFh3x
Android: / @tutorialseuandroid
C#: / @tutorialseuc
Unity: / @tutorialseuunity
Facebook: / tutorialseu-1093802040...
LinkedIn: / tutorialseu
Discord: / discord
🚀C# Progress Academy - Become a senior C# developer: academy.tutorials.eu/p/csharp-progress-academy
Kind sir, you made a one-hour video tutorial in just 8 minutes.. Thank you.
This is the most clear and concise explanation of implementing a service through dependency injection I have seen!
yes
thank you!
Of all the DI tutorials I watched, this one finally made it make sense. Thank you!
You've helped me learn something I've been stuck on for a WEEK. THANK YOU!!!
Hondestly, after reading 3 entire Days of documentation, this tutorial did the job in 8 minutes...
Great one!
very simple and clear! thanks dude!
Awesome explanation. I've been trying to get my head around this concept and this finally did it for me thank you!
This tutorial is very clear and understandable even for a newbie like me! Thank you very much!
Short and sweet but full of important information. Thank you for the video!
thanks
The first video that has actually helped me understand this concept!! Thank you
that's awesome!
Well explained in a very short time. Thanks
this is very good, short and simple to understand.
Short and clear, thank you.
Very simplified. I love this
Awesome content .. loved the explanation!
Very well explained thank you so much
Man you are amazing, explained those concept in real quick. I would say, every single word is really important to me hope everyone else feel the same.
thanks! glad you like it :)
Thanks for class!!!
This was an excellent and simple way to understand how to create a service and implement it from DI. Thank you very much!
Cheers mate nice and concise
Thank you for keeping things short and precise
Sure :-)
Fantastic!
Perfect, simple and concise video. Thanks!
You're welcome!
That's what I was looking for , for a long time...I was struggling to understand....Thanks buddy...you are geeat😊
This is the first video I watch regarding Dependency Injection, and it’s really clear and helpful.
Glad it was helpful!
Nice, simple and precise, thank you!
Sure
Really nice and easy to understand what the dependency injection is.
great tutorial, concise and clear! keep sharing!
very good explanation of a complex topic. thx
This is the clearest video i have seen in my life. Thanks
sounds good
Amazing presentation. I understand this concept in 8 min rather than reading the same in the whole day . Keep posting this type of critical videos and we will definitely watch you❤
You are genius. Simple and easy to understand. Please work on delegates in c#
Thanks a ton 🍻
thank you it is really helpful
Solid and to the point. Thanks.
sure!
You are awesome! Very helpful, thank you
Sure thing!
Beautiful! I am now a subscriber.
awesome! :)
thanks, This is very good. I need all the information exactly what i needed.
that's awesome! :)
Good explanation. Thanks
You are welcome
Thank You!
Thanks!!
greatly helpful
Glad it helped
Thanks
Thanks😃🙌
Hi bro, your explanation is really simple and clear. Also, practically show how to do it. Appreciate!🎉
thanks mate :)
superb
thanks!
thanks
Nice job, if possible do a side by side comparison with Dependency Injection in Blazor Server App vs. Blazor Web Assembly App. Ty!
Good video. I'd love to see you include some of the extra info like advising people to use some sorts of automated service registration tools like AutoFac as this will save their time and hassle as the application grows with more services to be registered line by line.
super good video! One thing I am trying to figure out right now. I get that we make the interface and add the interface to the "Dependancy injection system" But when we go back to the controller..... we set a variable [interfaceType _Name] then in the constructor we pass in [interfaceType Name].... Now when I step though the code I See that it's getting the info from the Dependancy injection I just don't understand how? I am missing how passing it into the constructor connects to the dependancy injection register thing. hopefully that made sense lol.
D best. SImple and clear
Wow thanks for simple explanation what actually the DI is for. Would love to watch email sender implementation or another topic: how to perform lets say monthly activities i.e. e-mail sending. Regards!
Great explanation! Thumbs up ! Request fir a tutorial ! A complete Register and Login with Forgot and Reset password ! / Role based authorization! / would be much appreciated! 😊
Thankk
Thank you, short and clear. Btw would you make videos about clean architecture and mediatr as well?
Yes, definitely
Is it possible to make multiple service for one controller?
This video is great very helpful. One thing I need to understand better is the concept of services and containers. Less about the code and the implementation in .net and more about conceptually what a service is, what a container is. I'm aware that it's all for the purpose of dependency injection, and I understand dependency injection, but services, containers, and app building are foreign concepts for me.
Thanks a lot for this feedback! I will put that on our todo-list so that we can create a video regarding services and containers in asp net :)
Thanks for sharing. But all the examples on UA-cam are without existing an abstract class having method implemented, also if the service have properties.
Peace upon you , I have start learning .core .6 with c# and trying to do comment with rating and admin dashboard from scratch so since you mentiond to comment on the video about something to make video about it I droped this comment ...
Please i have been looking for this in nestjs, does anyone know of an approach
Can someone help out with the namespace to be implemented in the program class? Take for instance like the IEmailService he used. What would be the namespace in the program class?
I believe that all files beneath a project in a solution, that that project name as their namespace
Nice and simple ! please I have a question there.Do we really need to use Autofac in .Net 6 ? if yes/no would you please explain why ? thank you for your reply.
Hi, there! Glad you like it.
This is a questions, that's not so simple to answer with yes or no. Take a look at this autofac migration documentation:
autofac.readthedocs.io/en/latest/integration/aspnetcore.html
Cheers :)
Please make a video for make a c# library and use in c++ projece or other language.
What if we want to use it in a seperate class not a controller. Can you explain that as well?
Need a video explaining Asp.Net Core MVC Razor Pages!
will create it!
Is creating an interface necessary? Can't i inject the class directly?
What, if we have several services? Should I add them to the constructor? And what if my service also has a constructor where I should add the parameters?
You can pass more services just need create a field and add them to constructor !
Ok, but what I want to know is how you register nested services in the Program.cs
please have a tutorial for rabbitMQ
What is the difference between tutorialsEU - c# and TutorialsEU channel . Are those two done by the same company
The best way to upload an Excel file into database using Razor pages
Ran into an issue with DI and using it outside of the pipeline (say, outside of a controller). No one seems to know how to use DI this way nor how to use it with a class library. Been told it's not possible, but I just can't believe this to be true.
Using it with a class library is the whole point of it.
@@scmsean - That's what I figured. I'm trying to use DI outside of a web application is all, but looks like I need to do a lot more work or reference something else (DotNet6). It should be simple, but tutorials either gloss over the important stuff, only use MVC, or are out of date.
@@davestorm6718 Tutorials usually just show you simple stuff. They never help you with real world stuff. Since they are showing you simple, they are never are setup correctly.
@@scmsean Agree! There's a lot of them that simply do things the wrong way, as well. Trying to find the "correct" way (in my case the Microsoft way), has been difficult - even the books online reads more like an index than a step by step guide, leaving more questions than answers. MS also keeps changing the game - they ditched the startup.cs, for example, and black-boxed a lot of things (EF is one example - if EF was more efficient, I'd still be using it - but it's still slow no matter how many optimizations you apply, terrible for microservices, insecure via sp_ExecuteSql , full of bloat and makes excessive calls to a database. Sure, it's easy, but the price is too high! - you spend days tweaking it then realize you could have just written DB code in ADO.Net - or used an ORM generator - and been done with it - faster!)
I did a bulk insert with 10 million records: in raw ADO.Net code - it took 14 seconds to run, in highly optimized EF Core, it took a full minute!)
Ich könnte ein einfaches Beispiel gebrauchen, über Account Registrierung und Logging mit SQL entity framework und dependency injection
me, a 100% beginner watching this out of context with no idea on how to program in C# or what anything here means: ._.
SignalR for aspx
My english is very bad, but i have understood every your word. Great pronunciation. Thanx, you're awesome!
Hi! Great video, you helped me lot here with a problem in injection on Startup. I apprecite if u can help me out with a problema that I'm having rn when I run my Application.
"Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)"
That erro appear to me in swagger, and I'm search for a long period of time and I did not found a solution, can you help?
Thanks!