Can I Run .NET 7 Minimal API in a Windows Service?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there and welcome to the code wrinkles channel so can I run a.net 7 minimal API in a Windows service let's find out so to do this I have created this blank solution where I don't have really anything but I will do here is I will simply add a new project now there are several templates here in when it comes to asp.net core and also you will find these templates or similar templates in Visual Studio but what I would like to look at right now is this type of worker service and I will give this name file parser the core concept of a worker service is that there is simply some work that we execute in background and here this is what it comes from the template is just log some information and it waits for a second then in program.cs we see that we have a host here created for plural configure services with configure a service to add our file service and Dot build now the thing here is that by default this worker service is just a worker service but it's not a Windows service so we need a way to transform this into Windows service to do this we'll need first to install a new get package and that nuget package should be this Microsoft extensions hosting Windows service when this is done I would like to further configure this Windows service a little bit and to do this we have this method on the previously installed package which is called use Windows service and here we have an options action in which we can provide a lot of different things and one of the things that we will use for now is the service name and I'll call this a code wrinkles service and that's virtually everything that we need to do to run this as a Windows service however I want to make this as practical as possible and I would like to change this a little bit one critical situation when I needed to work with Windows Services was for instance when we need to process some files so there were other parts of the system that were placing some files in a certain location and we need them have processed in regular time intervals so to achieve this will also go here and let's say here edit and rename and we'll call this file parser because this will be our service it will just parse files now since we've done this we want to also change it here and obviously we need to make these changes in a few other places like also the Constructor here and also last but not least here in this registration yeah and since I noticed here that I have named this project file parser like the service we need to use this fully qualified name when we want to add the hosted service now let me come back here to the service and Implement a very basic file parsing logic to make things easier for this demo I would simply add here a constant folder folder but where I have previously prepared two files and these are the files that are already there now what we can do here is we can get get rid of all the entire logic here and Implement our own so first of all I would like for instance to wait for 5 seconds because I want this service to run in interval of 5 Seconds then I can log some information the departing is started and last but not least I would enter it through all the files that we have in a certain folder in this case we will just delete the files and we will look that the processing has finished for a certain file name now to make this run and deploy it and use it as a Windows service there is here also a second part to it and that part is the publishing so let me go here and click on publish and I will choose local folder here one important thing to note here is for the deployment mode I usually tend to also want to use self-contained when I plan to use this as a Windows service if you have it as a framework dependent it would kind of like depend on the fact the dotnet 7 should be installed on the server or on the machine where you want to run the windows service and since you cannot always control exactly what install there I prefer to have the self-contained version that contains all the binary that it needs to run and it doesn't rely on anything being installed on the Windows machine or Windows Server now here I would like to also choose Winx system 64 because that's what my operating system is and there is one just two more options that we have to click the first one is this enable ready to run compilation this will compile everything in a way that it is ready to run and then also produce single fight and this will ensure that all the binaries and everything is needed will be placed together in just one single file so that we can install that and use it as a Windows service and once this is actually done we can just simply run and publish this application okay so now the application was published but it is actually not a Windows service yet one thing that we need here is we need to copy this folder structure here the next step that we need to do is we need to make this or install this as a Windows service and for this I would use this command line console and we have a very nice tool which is dot sc.exe to which we can interact with the services that are installed on the current machine what I will run is this uh create and I'll name this service file person and then you will have to specify a bean pad that contains exactly the location of the exe that you want to use as Windows service in our case it could be the exe that was generated after we have published it now if we press enter this will create this as a service starting the service is actually very easy because we just need to use this start command and specify the name of the service that we want and here is this file parser so we have pressed enter and here we have the confirmation that it has started and if we look in this folder the two files that we previously had here are gone but let me copy them again and we'll see that just in a few seconds gone again so this means that this service is not currently running so I have basically a.net 7 application running as a Windows service but I'm sure you will say but hey that's not a DOT Net 7 minimal API and that's actually true so let's do something else let's adhere a new project and this time I will check from here asp.net or web application and I will leave this empty and this will create me a minimal API obviously I also need to give a title and it would be this Hello World Service so let's create this project from scratch the result here is a regular mini minimal API with an endpoint and we will keep this and make sure that we can call endpoints that are basically hosted or running in a Windows service but to do this we need to First install some nuget packages and in this case we actually need to install two of them so first we need the same Microsoft extensions hosting Windows service that we have installed also in the previous project but then we also need a Windows service version for the host asp.net core and in this case is this package Microsoft SP dotted core hosting Windows service so let's also add this package to our project things are here a little bit different than in the previous example because we still need to run this as a asp.net core application but we need to run it in a Windows service so we need to keep the web application Builder because that that's what it helps us to make it a web application but then what we do is we can say on the Builder on the host we can say this use Windows service which will add the possibility for everything to work as a Windows or from inside a Windows service then we'll also need to make sure that we add Windows services to our service collection extension because it will be needed for different registration with the different parts of Windows service and that's basically everything that we need to configure here we leave this map get on the home so that we can get Hello World back however before we run this there are some few other things to consider first of all we will need it to also add or edit the cs plus file and the reason for this is because we need to provide here a runtime identifier in my case I will provide Windows 10x64 because that is my runtime identifier of the operating system I am currently working on then I would like to go over to app settings.json because there are some things that I would like to Define here and one thing that I would like to do is to end also the event log as a logging sync basically for my application so that every minute I log from the application is also logged in the event log and I can check it out there but then there's also the second part so I have here a regular asp.net core application with the.7 minimal API but usually to run this you would need to run it on a web server and you would usually have it on an IIs server if you're in a Windows environment and you don't really have to take care about anything else about the routing about how requests are handled because everything is handled by the web server and just sent over to your asp you don't Encore application for processing however when we host it as a Windows service this is not possible so what we need to do instead is to use the built-in web server of asp.net core which is Castrol to actually Define some endpoints to which cash flow should listen and this is why we have this configuration here which is called Kestrel and then endpoints and we will use HTTP and I will use as an endpoint this localhost 5421 now there is one very important thing to note here we need to use this HTTP endpoint and we can't use https for these demo purposes for to use an https you would need a certificate and then you would need to import this certificate in your Dot and core application and kind of like work with it and make sure that you use this certificate for decrypting https traffic so we don't want to have this complexity right now so we'll keep just simple HTTP traffic here so now that we have everything configured we can once again go here on publish and was once again local folder and we'll use the exact same configuration so I will use self-contained deployment enable in ready to run compilation and I will also enable this produce single file and then we should be good to go and we can simply run everything and make it publish okay so we are done I will also copy this publish pad so now we need to do exactly the same thing as we did previously and create a new service for this and to create this new service I will just go here and just reuse this command that we have previously I have just updated a bin pad to lead to our hello world service now the only thing that we need to do here is let's also change the name of this one to be hello instead of file parser and let's create the file and that was now created now let's for instance go here and let's say start and we need to start the hello service so we will have just hello here and here we'll replace this delete with a start okay I guess I had just a tea that was too much so let's start it again and we can see that now the service is started so theoretically now we should be able to access this service through HTTP so the URL to which this service should be available should be this localhost 5421 so let's click here and you see that we get this hello world back so it means that we have managed to actually deploy r.7 minimal API inside the windows service and we can prove it that it works because we can access the URL that we have specified in the application and that is in this case served by Castrol the built-in web server that we have in asp.net core or a practical reference this type of services is very useful whenever for instance you need to have something like HTTP triggers and you can't easily use Azure functions because maybe your company or your customer doesn't want to choose either functions and there are really a lot of different scenarios when you might want to actually do this and expose certain HTTP endpoints through which you can Kickstart certain operations for instance for instance in a larger infrastructure operation when something happens in the business and a lot of things need to change probably there is a Powershell script that needs to be executed and then you can simply have this service basically expose an end point that will just be a trigger for executing some powerful service you can use.net to execute Powershell scripts that you have already created so that's totally possible and in fact this idea for this entire video came from a discussion on our code Rico's members Discord server where we have a very good member which comes basically from the infrastructure side but does a lot of Dev to make his life easier as an infrastructure guy all in Discord his name is Johan and we had a very nice discussion about how he is using dot Net 7 minimal API to expose it as a Windows service and some trigger and expose some triggers and end points that would execute power Trail and in fact I will leave you in the description of this video a link to one of his repositories where you can find an exact example on how you can use or run powerful when you get a trigger from an HTTP request and an executed Powershell from your dotnet applications so you can just check that sample out don't forget to hit the thumbs up button and like this video if you think that it was useful for you and if you're for the first time here hit the Subscribe button and also the notification Bell so that you are always notified Whenever there is something you hear on this channel if you have any question or if you just want to get in touch with me don't be shy and head over to the comment section and leave your comment I would be more than happy to get in touch with you this being said thank you very much for watching and until the next time I wish you the very best
Info
Channel: Codewrinkles
Views: 5,798
Rating: undefined out of 5
Keywords:
Id: piwXq8MY4MQ
Channel Id: undefined
Length: 13min 25sec (805 seconds)
Published: Fri Aug 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.