.NET Core Console App with Dependency Injection, Logging, and Settings
HTML-код
- Опубликовано: 17 июн 2024
- .NET Core has brought a lot of great benefits right into the .NET language. We get dependency injection, a much better configuration system, and we get a common logging system that is easy to use. The only problem is that mainly, we only get these things set up for us in web environments. What happens when we want to get these things running in a Console app? Well, it takes a bit of extra work, but it can be done. In this video, we are going to set up dependency injection, configuration using appsettings.json, and logging using Serilog in a Console application.
Source Code: leadmagnets.app/?Resource=Bet...
Mailing List: signup.iamtimcorey.com/
Purchase Courses: iamtimcorey.com
0:00 - Intro
1:49 - Creating Console App (.NET Core)
3:35 - NuGet references
7:29 - Editing csproj file in .NET Core
9:15 - Add application settings json file
10:46 - Define goals
11:30 - Builder setup: BuildConfig
21:12 - Serilog setup: LoggerConfiguration
25:04 - Host setup: CreateDefaultBuilder
30:11 - Program entry point Class
39:51 - Dependency Injection
43:04 - Calling entry point in Main()
45:21 - Configuration for Serilog in appsetings.json
50:10 - Runnning the application and fixing exceptions
55:02 - Recap, summary and concluding remarks
Thanks to Ralfs HBK for the chapter breakdown
When I see the "I" in the channel's name, I always think it's an interface.
lol
Truuue
Lol so true.
public class Myself: IAmTimCorey
{
// Now I can do whatever Tim can do...
}
@@ibrahimhussain3248 For that you should derive the implementation rather than just implementing the interface... because you could say Throw new UnimplimentedMethodException() for each method.... lol
uowwww!!! great content as always, tim!!
my skills have grown so much since i´ve started following your videos, I usualy watch them several times and they´re my first consulting material whenever I get doubts during daily job....
I am glad my content has been so helpful.
Thanks, Tim. I think your videos is the most useful upon all other programming tutorials on RUclips. Especially for people with some experience as a programmer.
Great to hear! It is good to know that the training is valuable to folks with your level of experience.
So well put, I almost never come across a video where you follow tutorial step by step and it works, usually something is missing, something is forgotten
thank you!
You're welcome!
Bro your videos are AMAZING. You are covering the topics that we want to learn about, but other people are not covering!
Thank you!
This one had a lot of great information in it. Thanks Tim! Keep them coming!
As always a very clear, tight explanation of how things work in the .NET world. Thanks.
You are welcome.
I actually figured this out on my own when playing one day. However, there are tons of other things I learned in this video that I wasn't aware of. Subscribed!
Excellent!
I especially enjoyed this video.
I enjoy most of these videos, but I especially enjoyed this one.
It is greater to watch this, even if you never intend to write a console app, and explains the DI that gets used when writing an ASP/Web app.
I really like how you will always say "Don't do this in production" and then go back and fix it.
It can get a little frustrating when going through tutorials that say things like that, but never show you how to actually do it 'properly'.
Yeah, it is convenient to shortcut things since they aren't the purpose of the video, but I hate that too.
Your content's trustworthiness has become my go-to, when looking for C#/.Net subjects.
Thank you!
Thanks Tim, you are a great teacher. The way you explain things makes it much much easier to understand and adapt.
You are welcome.
Yes. I would very much like to see the winforms and WPF version of this.
I will add it to the list. Thanks for the suggestion.
@@IAmTimCorey I would love that too. I started with the TimCo video of appsettings, but since I didn't finish the entire TimCo Video list, I didn't have any of the coding in place that you altered, which made it abracadabra for me. :) This video is awesome, am curious how well this translates to WPF project (from scratch). :D
Thank you for making the time to making this video!
Great stuff, appreciate the time and effort you put into this. Really high quality material.
Glad you enjoyed it!
Thanks Tim for teaching us dependency injection in a very simple way !
Thanks, Tim. This video helped me a lot. I liked the underscore tip. I've been wanting to figure that out for a while. And using Serilog looks very promising.
You are welcome.
Wonderful! Thanks so much Tim. Trying to get up to speed quickly with C# and I learnt so much from this video.
Fantastic!
As always, extremely useful! I recommend this channel whenever I can to my colleagues and friends!
Awesome, thank you!
This video is just the epic way to understand complicated things. Well done.
Thank you!
Great video, thank you for taking the time to explain a few of the 'simple' things. If you don't know a few of them good enough, they are not simple
You are welcome.
This was a REALLY cool thing to watch... just like 2 weeks ago i was trying to add Dependency Inyection to my console app but i was looking forward to use native microsoft stuff.. Thanks for this!
Glad it helped!
Thank you so much for this!! I discovered this video after struggling with migrating some code from the Framework to Core. The video demonstrated the functionality in a way that I could easily internalize and use. Subscribing to the Patreon is the least I can do. Be well!!!
Glad it helped!
You have a great and simple way of explaining things. Very clear.
Thanks!
Cheers Tim. That is what I was looking for! Great film.
Excellent!
I love the refactoring option (underscore) update...I have been using that for a while now...just popped back as I had lost it...oh and the content is great too ;o) Thanks for the great content Tim.
You are welcome.
I was looking for a video like this. Thanks to you. I was able to understand and achieve this.
Awesome!
The copy output directory tip was clutch! Exactly the issue I was having. Great video as always. Thanks
Glad it helped!
I’m happy to see this. I’m a developer who primarily works on windows worker services / windows services, and a lot of backend applications where there is no user interaction. A lot of people forget that there are a whole realm of developers who aren’t primarily working on web.
Yep, I see you.
Thank you Tim! It is helping me understand this concept in detail.
Glad it was helpful!
Thank you, Tim. This is exactly what i have spent the last day trying to understand. I should have looked at your video hours ago. lol
I am glad it was helpful.
I so happy that you and I are living on the same time!
Thank you!
You are welcome.
First of all excellent videos!. I wanted to add that at 38:00 another very good reason to pass parameters separately is to avoid the string manipulation if our log level is higher.
Thanks for the tip.
When you talked about how reading the error message helps, I remembered some really bad moments I have had with a certain C++ compiler xD I think the C# compiler generally does a good job though, as you say, so that's a relief. Thank you for the video, it's great as usual :)
You are welcome.
Thank u for this Video, learning EF Core and DI in Blazor, but seeing this in Console is awsome.
You are welcome.
Thanks for sharing this, Tim., very well explained!
You are welcome.
Very well laid out Tim. Thank you so much and you are right, Serilog is a good tool and with your code right here, anyone can be up and running in a few minutes.
You are welcome.
This was a magnificent video and helped me a lot.
Thanks for your hard work and your awesome channel. Keep up the good work and thanks a lot :D
You are welcome.
It's funny, I just needed this and you posted a video about it. AMAZING!
Hope you enjoyed it!
@@IAmTimCorey Same but for a Windows application so doing some changes after watching video
This is a fantastic videos that shows how to do dependency injections, logging etc. Highly recommended to all.
Thanks for trusting Tim for your training.
Hi Tim, this was awesome as always. Thank you!
You are welcome.
That's a great start for Console applications
Thanks Tim 👍
You are welcome.
You are brilliant , you have explained and cleared my doubts in such a beautiful manner
Awesome!
Tutorials like these are little gems from this man.
Thanks.
thank you for the thorough and well explained video! you just earned yourself a new subscriber :) more power to u sir!
You are welcome.
Tim, this is pure gold:
var svc = ActivatorUtilities.CreateInstance(host.Services);
Save my life on a few console application that I'm now refactoring to hookup DI properly or the right way. Thanks a bunch.
You are welcome.
Can you please explain :Why cant we we get the instance of ServiceCollection, then a ServiceProvider and use the "GetService" method on it to get the instance of the GreetingService.
Yes that should work like this:
var svc = host.Services.GetService();
Really great video Tim. Thank you.
You are welcome.
This was so wonderful, thank you Tim.
You are welcome.
Awesome lesson, Tim! its very useful for me. Thanks!
You are welcome.
This is an excellent tutorial. Thanks, Tim!😊
You're very welcome!
Cool boiler plate Tim, thank you!
You are welcome.
Wonderful content, thank you Tim.
You are welcome.
I love your tutorials step by step, thank you for share your knowlage :)
You're very welcome!
Brilliant! It helped me with a coding assignment for a job.
Excellent!
Amazing as always, thanks Tim
You are welcome
This was AMAZING! Thanks Tim!!
Thanks for expanding your skills with Tim.
As always, very good content!!!
Thanks!
Dude, what a great video. I'm going to check out your site.
Please do! IAmTimCorey.com and RUclips channel ruclips.net/channel/UC-ptWR16ITQyYOglXyQmpzw Purchases directly fund my ability to provide the free content. Thank you for being a big part of making these free videos available.
Thanks for the underscore tip; that will save me some time. I recently implemented something like this using AddHostedService to register the operation class (GreeterService in your case), and host.RunAsync to execute it. The trick with this approach is to call IHostApplicationLifetime.StopApplication at the end of the operation to terminate the process.
Great tip! Thanks for sharing
25:54 I totally agree! It was so pointless to look at Microsoft documentations a few years back. Now they even have little walkthrough tutorials, although for me to read the documentations can sometimes be a bit overwhelming. So tutorial videos like yours are still very precious. 😅
I'm glad I'm able to add to the body of knowledge.
Thank you! Very much, very good consideration, finally i understand DI.
Glad it was helpful!
Great tutorial :-) Tim treats wachers as human beings: inteligent and smart, because he is very inteligent and smart and tries to predict eventual question or request and imediatelly answer to it. What a great guy :-) But without self-exaltation.
I appreciate the kind words.
Best explanations on subject as always!
Glad it was helpful!
wow, this is exactly what i looking for in the morning
Great!
INCREDIBLE. AMAZING. LIFE-CHANGING.
THANK YOU TIM.
YOU HAVE SAVED MY LIFE.
🙃
I'm glad it was helpful.
@@IAmTimCorey THE MESSIAH HAS REPLIED. WHAT IS EXISTENCE.
Another great video, thanks a lot for sharing your knowledge and time,
You are welcome.
Tim, thank you so much, this is so usefull. Additional thanks for some tips and tricks.
You are welcome.
Great lesson, good examples, thanks a lot!
You are welcome.
Excellent explanation as always
Thank you!
Thank you for sharing. It is very interesting and useful.
You are welcome.
Dude I have learned so much from this channel
Awesome!
This video helped - thank you!
You are welcome.
Thank you very much, you make very great content!
I appreciate that!
Good video. Just a couple clean up items that were forgotten:
1. *host* is an IDisposable, so you should wrap it in a using in your main method or call Dispose() manually. This way it cleans up every dependency that you might have living in your dependency injection.
2. It's best practice to call Serilog's Log.CloseAndFlush() method at the end of your program since you created the logger outside of the hosting environment. If you configure Serilog in the .UseSerilog() method, you don't have to do this (assuming you are disposing host properly).
You are right. I meant to include the CloseAndFlush at the end and totally forgot to come back to it. Thanks for pointing these out.
Excellent! I wasn't doing anything for such toutorial :D
Thanks for viewing and sharing you impressions.
Great video. I think showing how it is used in a WPF MVVM app would be great as well.
Thanks for your vote.
Realy great video! I would like to see a WPF example too. Thank you!
This.will.be.hard.to.chew.
Thanks for the explanations!
You're welcome!
Awesome video Time. Thank you so much
You are welcome.
Best explainer on the internet!
Thank you!
Thanks, it's very useful ^^
You are welcome.
So enjoyable and clear.
Glad you enjoyed it!
Very nice and useful tutorial
Thank you!
Hi Tim
I really appreciate you doing ML.NET in C#. There are very few tutorials out there, and they are not explaining things well as you. Hope you do ML.NET series :)
I will add it to the list. Thanks for the suggestion.
I think I need to subscribe.
Thanks for the lesson.
You are welcome.
Hi Tim 🙋♂️
I really love the way you explain.
I have a request. Can you make a series on different types design patterns. I know we already have one video for singleton design patterns but there are other too.
Thank you 😊
They are on the list.
Это великолепно, спасибо большое! => It's great, thank you very much!
You are welcome.
Wow. That UnderScore tweak from 33:57 is brilliant! Never knew this, and never understood why this isn't the default
Tips to make coding easier. I thought he just made LEARNING easier!
Excellent video.
Thanks!
I would love to see a continuation of gRPC that goes more in depth, i cant really seem to find any good advanced guides for c# on gRPC including things like Authentication, Authorization, Security, sending caller information metadata, logging etc etc. That would be great! Thanks for all the great stuff you put out, i was just working in my brand spanking new gRPC console app and thought, hmm how do i get dependecy injection working and i found this :)
I will add it to the list. Thanks for the suggestion.
I really love your videos. My only contention (and this is just the unfortunate reality for myself) is that I don't have access to Nuget packages at my workplace, whatsoever. I still learn plenty though.
That's rather unfortunate and something that I would recommend you talk to management about (you probably have already but tell them Tim says they should change their stance). NuGet provides you with pre-built pieces of code that allows you to not reinvent the wheel each time. Further, .NET Core itself relies on NuGet packages to add to it. Instead of making one bloated system, Microsoft created NuGet packages so that we can add just the things we need.
Tim I cannot give so many thumbs for all times I see this video. :)
Thank you!
Awesome video!
Thanks!
Thanks Tim!
You are welcome.
Awesome, you have a very nice didactic
Thank you very much! I try.
It would be awesome if you could do a video on how we would best create Unit Tests for such a project and test the services that are expecting the ILogger and IConfiguration to be dependency injectected.
Tim has done multiple videos on Testing, but I will add the suggestion to his list. Explore these to see if they address what you need - ruclips.net/user/IAmTimCoreysearch?query=test
Interesting approach I usually use service providers in combination with service collection to achieve Di outside aspnetcore
Thanks for sharing
Great Video Tim, One question, when the service creates the instance of GreetingService How does it know what values to set the constructor parameter objects to? Can you specify them?
Great Content
Thanks!
Excellent!!
Thanks!
Fantastic... thanks
Most welcome
Thank you for a great video. Please make a tutorial on how do we set up these services in a WFP application?
Great video, as always. Just a small nitpick.
I wouldn't recommend choosing "Copy always" as a copy option for a file. This forces the project to rebuild every time you build or start the application. This is especially annoying in libraries as they and their dependencies will always be rebuild instead of only when the content of any file changes. "Copy if newer" will almost always suffice except in special cases with dynamically generated files.
Thanks for sharing.