C# - How to Create a Windows Service - Part 1/3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so to create a Windows service is actually quite simple first thing we do is to create the windows service project and for that you go to new project and then there is a nice template in here called window service I'm just going to leave the name as the default windows service 1 and I'm going to click OK in the window service template project there are two files in here let me see if there are any references yes so what this template does for you it's to create two class files and also add the sis system that service process reference to the project I am going to click on the service one dot CS and right click actually and click the view code and as you can see this is nothing but a class that inherits from the service base class and also has two functions that are overloaded one is the on star function and the onstop function the two main functions needed for the window service to start in stock and inside the program the CS this is actually the just like a console app has a main function and that main function it's what the the window service the yet the window service application calls to start a window service and we're going to go over how to debug this as well so as you can see this class that inherits from the service base has a little bit of a different icon but don't let that throw you off it's not it's not that different from the program class and I believe that if I actually if I add a class here just a regular class so class 1 SES and if I inherit from service space let's see I'm not 100% sure if this kind of work or not enter resolved use a system to process and let me save this real quick I don't think I need to save I have to service space and there it is you have a class one with the same icon as the service one it doesn't have the designer attached to it but you get you get what I'm saying I'm saying that the service one does CS is pretty much a class that has been overloaded with something extra to is so the nice thing about using the template is that that Windows service already has everything in it so I'm going to go ahead and then the window service template has everything in it I'm going to go ahead and get rid of class one because we don't need it I just wanted I was just trying to prove a point ok back on track the next thing that we need to do so I'm going to click on service one here and I'm going to go to view code we have our start in stock methods here already override it so um let's do this I am going to create a file when this starts five and also actually going to tell you about something very interesting here I don't know if you use the environment dot current directory before which works just fine so let's just say that we want to put a file on start dot txt just to prove that we started the service so if you wrote other applications before you're probably familiar with environment that current directory which will put a file in the same directory as the executable the interesting thing about Windows Services is that they don't run in the same location as they are installed they run in the windows system32 directory so what needs to happen in here instead of using the environment current directory you have to use the app domain dot current domain the base directory okay let's just try that and I'm going to put a breakpoint in here okay and if I save this and if I try to run we're going to get an error but I just want to show you this real quick I'm gonna put a breakpoint in there start debugging the app and we're going to run into a bit of a problem here so as it tries to run you get this error that says Windows service start failure and it says that you cannot start a service from the from the command line or a debugger but there's a way around that and you what what windows wants you to do is actually to deploy the service to the your actual to your actual computer and that attach the debugger to it so um let's let's the default let's go ahead and then what a conditional directive here control I'm going to press ctrl KS and that's going to bring up some snippets of code and I'm going to use the if statement here if debug and then I am going to move this all the way to the bottom and by the way what i'm doing here i'm i'm creating a snippet of code that is going to allow us to debug the service and this is very helpful else okay so in here well in all des again just to debug the service and in service one i'm going to create a public function functioning here a public void on debug you can call this whatever you want i'm just calling you on the bug and then on the bug I'm going to call be on start function and I'm going to pass a null object to it and this on debug is going to actually call the on start function you can't call the on start function directly because it is protected I believe so let me go ahead and then just start a service one which is the name of our service called service one I'm going to create a new instance of it so my service equals new service 1 and then in here I'm going to do my service style CI yeah we don't have these on start out here yeah it's protect us so you can't see it so I'm going to call that on debug that I created myself and doesn't ask me to pass anything to it and also the last thing that we need to do here it's called system dot thread dot threading the thread that asleep and then in here system the threading dot that time out infinite so that's going to keep our service alive so now if we run again that one more time it's going to start the same point now instead of f10 over I'm going to press f11 so now I'm creating a new instance of the class I'm going to f10 this part and then now it's going to call the on debug function the one that we made and that's going to call the on star function and it's going to try to create the file one good thing to do here which I didn't it's to use the try catch block around any type of i/o functions that you're calling anytime that you're trying to read something from the disk or you're trying to write something to the disk it's it's a very good practice to actually put a try-catch round because you never know one what can be wrong maybe you can't access decimate you don't have permissions enough and you want to be able to handle that and not have your windows service blow up I'm going to Athol Evan again and now we're going back to the main function and that's going to put us to sleep as you can see right here we're still running okay so now going to solution Explorer I'm going to right-click windows service open folder and since we are in debug mode our files are going to be inside the bin debug and there you can see we want to start oh this is a good disappoint - so as you can see I am still debugging so I can't access that file which is actually just prove that it's still running in the back so I'm going to stop this and now I'm going to double click on start in that file that we created during the star service open open up just for just just fine okay so this is how you create a window service next I am gonna go ahead and explain how to install a window service thank you for watching bye
Info
Channel: Programming with Fabio
Views: 339,244
Rating: 4.8648276 out of 5
Keywords: C#, Windows Services
Id: uM9o8GsO_u4
Channel Id: undefined
Length: 9min 24sec (564 seconds)
Published: Tue Nov 20 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.