Intro to Windows Services in C# - How to create, install, and use a service using Topshelf
Вставка
- Опубліковано 16 лип 2024
- Full courses: www.iamtimcorey.com/
Patreon: / iamtimcorey
Newsletter signup: signup.iamtimcorey.com/
Topshelf Link: topshelf.readthedocs.io/en/la...
Services in Windows are really powerful tools for automation, but they are often overlooked. These small applications run constantly in the background and can be used for a number of tasks from the simple to the complex. In this video, we are going to look at how to create a service, how to run it and debug it, and how to install it.
0:00 - Intro
0:51 - Creating Console Demo Application
1:45 - What is a Windows Service
3:30 - Visual Studio service app template vs console app
5:00 - Topshelf NuGet reference
5:58 - Service app code design
20:49 - Running the Service App
22:07 - Installing and uninstalling the Service
26:44 - Recap
27:11 - Ideas for a Service Applications
30:10 - Summary and concluding remarks
Thanks to Ralfs HBK for the chapter breakdown
I don't know if you'll ever read this, I started learning C# about 2 weeks ago. I needed a new hobby for the quarantine and I figured that this one will do. You're honestly one of the most gifted people in passive knowledge I've ever encountered in my life, thank you for all the work you're putting in for us common people to learn. I can only wish you to never stop doing this for as long as you're enjoying this. Thank you very much.
I am glad my content is so helpful to you. Thank you for the very kind words.
if I had discovered this channel earlier, I would have saved myself a lot of headaches.
Well, I'm glad you found it eventually.
What an absolutely SUPERB video - so well explained and I wrote a Windows Service in less than 3 minutes after watching this start to finish. Absolutely brilliant. Thank you!
You are welcome. I'm glad you enjoyed it.
Thanks Tim! This couldn't have come at a better time. I just started to write a service on Friday and now I'm going to do it this way. Love your videos; they are so easy to understand.
Nice! I'm glad you will be able to make use of this right away.
I made a small service to routinely clean up my downloads folder like you suggested. Works great! You are inspiring me to "play with" C# more than I did in the past with all these great tutorials!
Awesome!
Excellent, just be careful with tasks like that, you don't want to be cleaning up your C:\ drive by mistake :)
I'm a junior c# full stack developer and I have to say Sir , you are a great help, not only make it easy to understand with step by step tutorial and also with a very clear and easy understand explanation. Thank you.
You are most welcome. Thanks for watching.
Wow, I'd heard about TopShelf, but never really looked into it, this makes debugging services so much easier - and looks easy to convert existing services to use Topshelf - Thanks Tim!
You are welcome. I'm glad it was so valuable to you.
Messing around with NSSM utility to run existing applications as a service, I decided to look for way to write my own service from scratch for more flexibility and to come up with a rock-solid approach that also allows easy debugging and testing. Found your "Windows Services in C#" tutorial -> bullseye! Thanks for your great work.
I'm glad it was helpful.
hey man, i have no words to thank you for some such i´ve learned from your videos this year. they´re always so complete and simple to understand.
I am glad my content has been so helpful to you.
cheers for this , i was trying to create a service using the windows service which wasnt pleasent, from this video ive adopted topshelf and its worked a treat. i now have managed to create a service that checks for USB sticks being inserted, and if usb has a serial that matches our data base it will check to see if ir requires a update,and if it does it then updates it accordingly. Great video Tim keep up the good work
Thanks!
Many Thanks Tim, I used to apply a do-while-loop to let a program keep running. I heard about the services as a best practice in such a circumstances but it looked a little scary to me. Thanks you again for make it so simple and understandable.
Have been waiting for this topic for a REALLY long time!!! thank you a LOT TIM!!! you are taking this channel to the next level... not kidding here!
Awesome! I'm glad you finally got it. You are most welcome.
Is it possible to have a service that accept parameters when windows starts?...
Interesting question. What are you thinking about? It would seem like if you could pass parameters in, you could do the same without needing to pass them in (maybe app.config settings) since you wouldn't be getting user interaction.
Working on a "listener" for a GPS hardware that will transmit information to my server which will store the data in an Azure Cosmo DB, so basically it will be great to pass for example, a specific port to use, max concurrent connections from a certain GPS or even "query" the service for uptime, current connected devices and stuff like that.
Yeah, I think updating the app.config or even a local database might be the solution here. As for querying it, you could actually just store that info in a local database (updated in real-time) and then read the database from anywhere you need that info.
Love the detail and simplicity of this video. Excellent.
Thanks!
WOW! I've been avoiding using services for over 10 years cause of the messiness of troubleshooting while developing. Today while watching your video, I learned how to, and proceeded to convert a simple console application to a service. Thanks a million!
Awesome! I'm so glad it helped.
Hi, I've been using services a lot, but they allways come with console sibling. My pattern was very simplistic. Entire job was done in class library and service in start/stop methods only called start/stop methods in class library. The same job is doing console application. Debuging was easy, just debug console application. And for fun, sometimes forget to reference some library in service application :)
Tim - thanks - I just finished your c# mastercourse (which was great!) and am embarking on my own projects to build a portfolio. I have a desktop scanner and a printer, and I'm going to create a service that watches the folder for newly scanned pdfs and then prints them - essentially a photocopier. Thanks to your helpful videos, I think I can pull this off.
Awesome! Great job and great idea.
Tim, Tim... You have been of immense help in my career. And this one is just Exceptional!!!
I'm glad I've been so helpful.
Thanks Tim, I am so glad that I came across your channel!
Thanks for trusting Tim when you need help.
The best lecture ever. I was Wondering on how to sync SQLite database files to the main server and I thought of checking how services work and here you explained it so clearly Thank you
Glad it was helpful!
You are a life saver. This was exactly what I was looking for. Perfectly explained and super easy to follow.
I am glad it was so helpful.
Thank you so much tim. This is an amazing video. Excellent explanation.
I am going to write a service that clears my temp folder periodically so that it doesn't slow my system.
Great! I’m glad it was helpful.
more than 15 years since my last visual basic class as a teacher and here I am learning a lot in a half hour video. I'm not that old but felt like 80y old easy.
Very good tip, love the intros and explanations (I want to understand, not just copynpaste).
sometimes its a bit too fast for someone that dont open a dev env since delphi 6 like me, those ctrl + ( . ) and tab autofill put me crazy sometimes, but nothing that a stop and 5 sec rewind dont solve.
thanks!
(sorry for my poor eng).
I am glad it was so helpful.
Crystal clear explanation. Exactly what I was looking for!
Great!
Thank you Tim. I did watch few different videos on this same topic. But this was simply the best. You explained the concept, the tools and the process so clearly and effectively. The idea of creating a console app instead of using the service project is brilliant. It saves so much time in debugging when we do a complicated service. Thanks mate. Much appreciated.
BTW, it would be great if you could do a follow up video on using Squirrel with this project.
Glad it was helpful! Thanks for the suggestion.
I'm pretty sure that you've listened this many times. You rock bro! His video helped to me so much, can you make a video talking about the services recovery options?
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
This was very useful!! I have to digest TopShelf into my brain a bit but I really appreciate the console app setup!! Thank you.
You are welcome.
This was a very helpful video. It truly gave me all the help I needed in getting a service stood up that runs once a day at 4 AM. Great job!
Excellent!
Always nice to come back to this even if for a quick refresher
Good to hear
Thank you very much! I am learning C# and i wanted to create a service for deleting a regedit folder if exists, i didn't know the language used on Windows services was c#. So this makes me learn it even more :D
Awesome!
Your content is some of the best out there on any subject. Thank you so much!
I appreciate the kind words.
Great! I was struggling trying to create a Windows Service using the template and this helped a lot.
Awesome!
Hi Tim! Excellent video as always! My best tip to debug an ordinary win service project is use of #if debug in program.cs and call to same "start method". It works perfectly for me :)
Yep, that works too.
Wooouu!! Tim u are the man! I am really happy to know to do this, I could do it on my own compu, its awesome !!! how easy you explain it, ! tons of gratitude. my first services!
Awesome! Congrats on your first service!
Thank you for this wonderful video. I was running into some serious roadblocks in rewriting a windows service at my current workplace due to the Microsoft project template making it difficult to debug initialization steps. Also, I totally agree with the text logging being an issue on long running windows services, since the person who wrote the old one was using text logging and it was blowing up the system despite changing to a new text file daily.
Thanks for sharing that real world example.
Thanks again Tim! I was thinking about saving the day to day wallpaper-pictures coming from bing somewhere to keep them (sometimes very nice pics) , This will be the way ! So many good things coming from you ! Best Regards !
Sounds great!
Super useful. I love your idea of using a service to perform file maintenance! :)
Thank you!
This video “inspired me” to actually implement the “download folder keeper” as a service, and I’m almost done, if any one is interested I will be uploading probably tomorrow the source code to github, I used what Tim tough in a video about directories but I also implemented a way to find the “default” download folder for your computer. Also used the “app.conf” as Tim also did as a challenge a few weeks ago.
Thanks once again Tim... you had built a great community to share knowledge and learn!
I'm glad you are making use of what you learn.
Okay guys... the project is up, all you have to do is download, compile and install. So far the thing is working for me!
github.com/acidrod/DownloadsKeeper
Such an elegant way of explaining things... Awesome dude
Glad you think so!
it's been a while i didn't stop here :) Happy i did today!
Thanks, usefull stuf (veeeerry usefull) well explained as usual!
I like that format you have for a while, quick snipet from A to Z with amazing (basic) ideas :) Thanks
Thanks for the feedback. I'm glad you are enjoying the content.
Great video, hope for more about services and communication between services and applications!
I can add it to the list.
Definitely deserves a like. However, I recently had to implement a service for work and I got the 1053 error when I started it. What it said was
"The service did not respond to the start or control request in a timely fashion"
Instead of writing the following start method as mentioned in your video
public void start()
{
//instantiate a bunch of objects and running through the constructors
}
I use start to call async codes as the following
public void start()
{
_callStartTasksAsync();
}
private async Task _callStartTasksAsync()
{
await Task.Run(()=>{ //instantiate a bunch of objects and running through the constructors } )
}
This allowed the service to get through the start method ASAP and avoided the error
i face the same problem
Excellent video!! Thank you very much for the quality content Tim
You are welcome.
Wow! Such a great tutorial and beautifully explained!
Thank you!
You are a great instructor Tim! Keep it up... 👍🏻
Thanks!
This is so useful and well explain thing to learn today, you are the best Tim
Glad it was helpful!
Very helpful. I always have trouble with brown outs in my area and I have a plex server running in my basement. If I am out of town, the server would restart but the plex software would not. I created a service called to launch plex when the server rebooted automatically. Works perfect.
Excellent!
Thank you for this great video. My goal from what I learned from your video is that I need to do check specific equipment on my network to make sure they are running and provide updates on the status of the equipment say for example every 10 minutes I would like my service to check the equipment every 10 minutes by doing a ping test to see if there is a return if there is no return the change the status code to indicate this. The information will update a database table as changes occur. I figured having running a Windows service is better than using a task manager to perform the job.
Sounds great.
Thanks a lot for responding to my comment. Now the font and colour are clearly visible even in my phone.
Excellent.
Thanks Tim! Did the job nicely and smoothly.
Thank you!
@@IAmTimCorey Hi Tim, I could suggest an idea to create a video showing how a service can get the permissions in order to launch applications as this was my initial goal. It appeared the OS sessions doesn't allow that by default but I read it is achievable.
Thanks for another great video tim!
I'm using trying to use a service to listen on a socket server from a large number of client connections. The goal is to be able to run a scheduler on these clients and take their payload to store it in a central database.
Great!
Great tutorial! It help me a lot with a project that I had to create a service that makes a data copy from Arduino to a MySQL database.
Glad it helped!
Great tutorial as usual. Thank you
You are most welcome. Thanks for watching.
Thanks Tim , so much to learn from your videos.
You are welcome.
Excellent explanation. Thanks, I was able to type along and understand.
Glad it helped!
Hello, I love your tutorials I want to thank you for those! It was very interesting! So I can build my own Windows Service project or install the service automatically. Thank you for explaining what everything does.
You are welcome!
Excellent tutorial, learned more than expected
Awesome!
Thank you, this video was very helpful. With this my project worked smoothly.
You are welcome.
This a life saver. It made my job so much easier, thanks for that!!!
You are welcome.
This works great. Got this Just In Time.
Very helpful. Thank you
Excellent!
Thanks Tim! Finally I can abandon my holy trio consisting of Windows service for production, console application for debuging and class library handling all the fun. Service and console app were very simplistic. Just start and stop Task provided by class library.
Nice!
great Video! I always Use Windows Service Project. When I do this, I always create a second project in the same solution with a simple GUI, to trigger the same methods as the service does, when I need to debug it. That also works pretty well, but maybe I will try this solution in the future.
Thanks!
Very good .. I was thinking about parsing a text file automatically to database . and Now I get it .. thanks for the tip
Thanks for looking to Tim when you need answers
Thanks a lot for the video, even now after these years help me a lot. Regards.
You are welcome.
Well explained. Thank you for sharing
You are welcome.
I really liked the idea of organizing pictures. I will use it for organizing my desktop.
Great!
Tim is a wonderful tutorial. Thanks
You are welcome.
Great video, nice introduction to a topic I knew absolutely nothing about.
I'd love to see a video on using Github within Microsoft Visual Studio for a simple project. The older one is good but think one using the actual baked-in functionality would be very helpful.
Something close to that is coming soon in a course. I'll be covering GitHub at some point as well.
Many thanks, sumptuous and very clear ...
You are welcome.
Amazing video, thanks for the help! Keep doing the good work, it helps us a lot! :D
You are welcome.
Amazingly clear!
Thank you!
Very nice tutorial I got my solution, Thanks Lot Tim and looking more from you thanks again
You are welcome.
You´re awesome, really perfect understanding!
Thank you!
Thank you! I will be making an API connector to check for updates in my SQL database table and post them via API.
Excellent!
Thanks, Tim, great video as always.
You are most welcome. Thanks for watching.
@@IAmTimCorey What version of NotePadd++ are you using? I couldn't find that "Monitoring" feature you mentioned in the video.
7.4.2 - I believe I'm actually a couple versions behind
Excellent. Thank you for this. I a web coder and needing a Services project to do SQL things. Big help :)
Glad it helped!
Be good to see a video that expands on this one to show a simple toolbar icon that allows you to start\stop services and maybe open a window that allows one to change a setting.
Thanks for sharing your code and knowledge. This will help me a lot... God bless!
You are welcome.
Great tutorial as always Tim! Very useful!
Thank you!
Thanks a lot Tim, you are the best
You are welcome.
thanks for the clear explanation.
You are welcome.
Very nicely done video, Thanks
You are welcome.
Amazing. Thank you.
You are welcome.
Thank you! This is very helpful!
You are welcome.
Wow this is crazy useful
Great!
This is amazing. Thank you so much.
You are most welcome. Thanks for watching.
Thanks for excellent video
So nice of you
Great video! We use this technique to export web services
That's great!
Thank you very much for your tutorial.
You are welcome!
In File Explorer in the address bar you can just type "cmd" and it will open cmd to that directory. Great video by the way, still trying to think of some ideas on how to use this!
Thanks! I didn't know that.
Very good tim. God bless you
Thanks, and to you!
Awesome Tutorial
Thanks!
Nicely Explained.
Thank you!
Very helpful, thank you!
You're welcome!
thankyou very much,it is a simple and good sample.
You are welcome.
Thank you very much. This video helped me setup my very first service using a language i am very unfamiliar with but now am building an idea of how to use. :)
Next trick... either counting the output files or switching file names when it reaches the next day? Hmmm ...
Awesome! I am glad it was so helpful.
Great tutorial. Thanks
You are welcome.
Thanks, Like your videos, simple and clear :-)
Glad you like them!
Excellent Video
Thank you!
A great video indeed , Ideas in the ending are amazing though.
Thanks!
Very useful, thanks!
You are welcome.
A million thanks. It's great.
You are welcome.
Tim , this is a great tool
Yep.